<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>CircuitPanel.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #000000; background-color: #ffffff; font-family: Monospaced}
table {color: #000000; background-color: #e9e8e2; font-family: Monospaced}
.comment {color: #969696}
.line-number {background-color: #e9e8e2}
.ST0 {color: #969696; font-family: Monospaced; font-weight: bold}
.character {color: #ce7b00}
.ST1 {color: #ce7b00; font-family: Monospaced; font-weight: bold}
.keyword-directive {color: #0000e6}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/matt/NetBeansProjects/trunk/ui/CircuitPanel.java</td></tr></table>
<pre>
<span class="line-number">   1</span> <span class="keyword-directive">package</span> ui;
<span class="line-number">   2</span> 
<span class="line-number">   3</span> <span class="keyword-directive">import</span> java.awt.Dimension;
<span class="line-number">   4</span> <span class="keyword-directive">import</span> java.awt.Point;
<span class="line-number">   5</span> <span class="keyword-directive">import</span> java.awt.Graphics;
<span class="line-number">   6</span> <span class="keyword-directive">import</span> java.awt.Graphics2D;
<span class="line-number">   7</span> <span class="keyword-directive">import</span> java.awt.Image;
<span class="line-number">   8</span> <span class="keyword-directive">import</span> java.awt.Rectangle;
<span class="line-number">   9</span> <span class="keyword-directive">import</span> java.awt.event.ActionEvent;
<span class="line-number">  10</span> <span class="keyword-directive">import</span> java.awt.event.MouseAdapter;
<span class="line-number">  11</span> <span class="keyword-directive">import</span> java.awt.event.MouseEvent;
<span class="line-number">  12</span> <span class="keyword-directive">import</span> java.awt.event.MouseMotionAdapter;
<span class="line-number">  13</span> <span class="keyword-directive">import</span> java.awt.image.BufferedImage;
<span class="line-number">  14</span> <span class="keyword-directive">import</span> java.io.File;
<span class="line-number">  15</span> <span class="keyword-directive">import</span> java.io.IOException;
<span class="line-number">  16</span> <span class="keyword-directive">import</span> java.util.Collection;
<span class="line-number">  17</span> <span class="keyword-directive">import</span> java.util.LinkedList;
<span class="line-number">  18</span> <span class="keyword-directive">import</span> javax.imageio.ImageIO;
<span class="line-number">  19</span> <span class="keyword-directive">import</span> javax.swing.JFileChooser;
<span class="line-number">  20</span> <span class="keyword-directive">import</span> javax.swing.JOptionPane;
<span class="line-number">  21</span> <span class="keyword-directive">import</span> javax.swing.filechooser.FileFilter;
<span class="line-number">  22</span> <span class="keyword-directive">import</span> netlist.properties.ButtonAttribute;
<span class="line-number">  23</span> <span class="keyword-directive">import</span> netlist.properties.Properties;
<span class="line-number">  24</span> <span class="keyword-directive">import</span> netlist.properties.PropertiesOwner;
<span class="line-number">  25</span> <span class="keyword-directive">import</span> netlist.properties.SpinnerAttribute;
<span class="line-number">  26</span> <span class="keyword-directive">import</span> netlist.properties.TextAttribute;
<span class="line-number">  27</span> <span class="keyword-directive">import</span> sim.Simulator;
<span class="line-number">  28</span> <span class="keyword-directive">import</span> sim.SimulatorState;
<span class="line-number">  29</span> <span class="keyword-directive">import</span> sim.componentLibrary.Circuit;
<span class="line-number">  30</span> <span class="keyword-directive">import</span> ui.command.Command;
<span class="line-number">  31</span> <span class="keyword-directive">import</span> ui.file.FileCreator;
<span class="line-number">  32</span> <span class="keyword-directive">import</span> ui.grid.Grid;
<span class="line-number">  33</span> <span class="keyword-directive">import</span> ui.components.*;
<span class="line-number">  34</span> <span class="keyword-directive">import</span> ui.components.SelectableComponent.Pin;
<span class="line-number">  35</span> <span class="keyword-directive">import</span> ui.command.CommandHistory;
<span class="line-number">  36</span> <span class="keyword-directive">import</span> ui.command.CommandStage;
<span class="line-number">  37</span> <span class="keyword-directive">import</span> ui.command.CreateComponentCommand;
<span class="line-number">  38</span> <span class="keyword-directive">import</span> ui.command.SelectionTranslateCommand;
<span class="line-number">  39</span> <span class="keyword-directive">import</span> ui.command.SubcircuitOpenCommand.SubcircuitComponent;
<span class="line-number">  40</span> <span class="keyword-directive">import</span> ui.components.standard.PinLogger;
<span class="line-number">  41</span> <span class="keyword-directive">import</span> ui.error.ErrorHandler;
<span class="line-number">  42</span> <span class="keyword-directive">import</span> ui.components.standard.log.ViewerFrame;
<span class="line-number">  43</span> <span class="keyword-directive">import</span> ui.file.JPGFileFilter;
<span class="line-number">  44</span> 
<span class="line-number">  45</span> <span class="comment">/**</span>
<span class="line-number">  46</span> <span class="comment"> * </span><span class="comment">This</span> <span class="comment">panel</span> <span class="comment">represents</span> <span class="comment">the</span> <span class="comment">visual</span> <span class="comment">workarea</span> <span class="comment">on</span> <span class="comment">which</span> <span class="comment">the</span> <span class="comment">circuit</span> <span class="comment">is</span> <span class="comment">drawn</span><span class="comment">.</span> <span class="comment">And</span> <span class="comment">responds</span> <span class="comment">to</span>
<span class="line-number">  47</span> <span class="comment"> * </span><span class="comment">mouse</span> <span class="comment">actions</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">user</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">circuit</span> <span class="comment">panel</span> <span class="comment">also</span> <span class="comment">keeps</span> <span class="comment">a</span> <span class="comment">history</span> <span class="comment">of</span> <span class="comment">commands</span> <span class="comment">and</span> 
<span class="line-number">  48</span> <span class="comment"> * </span><span class="comment">retains</span> <span class="comment">lists</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">components</span> <span class="comment">that</span> <span class="comment">are</span> <span class="comment">drawn</span> <span class="comment">here</span><span class="comment">.</span>
<span class="line-number">  49</span> <span class="comment"> * </span>
<span class="line-number">  50</span> <span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">Matt</span>
<span class="line-number">  51</span>  <span class="comment">*/</span>
<span class="line-number">  52</span> <span class="keyword-directive">public</span> <span class="keyword-directive">class</span> CircuitPanel <span class="keyword-directive">extends</span> javax.swing.JPanel <span class="keyword-directive">implements</span> sim.SimulatorStateListener, PropertiesOwner {
<span class="line-number">  53</span> 
<span class="line-number">  54</span>     <span class="comment">// Component Variables</span>
<span class="line-number">  55</span>     <span class="keyword-directive">private</span> Point lastDragPoint = Wire.DEFAULT_ORIGIN();
<span class="line-number">  56</span>     <span class="keyword-directive">private</span> Point dragStartPoint = Wire.DEFAULT_ORIGIN();
<span class="line-number">  57</span>     <span class="keyword-directive">private</span> Point currentPoint = Wire.DEFAULT_ORIGIN();
<span class="line-number">  58</span>     <span class="keyword-directive">private</span> Point previousCurrentPoint = Wire.DEFAULT_ORIGIN();    
<span class="line-number">  59</span>     <span class="keyword-directive">private</span> LinkedList&lt;SelectableComponent&gt; drawnComponents = <span class="keyword-directive">new</span> LinkedList&lt;SelectableComponent&gt;();
<span class="line-number">  60</span>     <span class="keyword-directive">private</span> LinkedList&lt;SelectableComponent&gt; activeComponents = <span class="keyword-directive">new</span> LinkedList&lt;SelectableComponent&gt;();     
<span class="line-number">  61</span>     <span class="keyword-directive">private</span> SelectableComponent temporaryComponent; <span class="comment">// Used for reference to a selection from list of drawn components</span>
<span class="line-number">  62</span>     <span class="keyword-directive">private</span> SelectableComponent highlightedComponent; <span class="comment">// The currently highlighted (SelectionState.HOVER) component    </span>
<span class="line-number">  63</span>     
<span class="line-number">  64</span>     <span class="comment">// State Variables</span>
<span class="line-number">  65</span>     <span class="keyword-directive">private</span> String currentTool = <span class="character">&quot;</span><span class="character">Select</span><span class="character">&quot;</span>;  
<span class="line-number">  66</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">boolean</span> nowDragingComponent = <span class="keyword-directive">false</span>;
<span class="line-number">  67</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">boolean</span> multipleSelection = <span class="keyword-directive">false</span>;
<span class="line-number">  68</span>     
<span class="line-number">  69</span>     <span class="comment">// Selection Box variables</span>
<span class="line-number">  70</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">int</span> selX;
<span class="line-number">  71</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">int</span> selY;
<span class="line-number">  72</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">int</span> selWidth;
<span class="line-number">  73</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">int</span> selHeight;
<span class="line-number">  74</span>     
<span class="line-number">  75</span>     <span class="comment">// IO and Editor variables</span>
<span class="line-number">  76</span>     <span class="keyword-directive">private</span> String filename;
<span class="line-number">  77</span>     <span class="keyword-directive">private</span> CircuitFrame parentFrame;
<span class="line-number">  78</span>     <span class="keyword-directive">private</span> Editor editor;
<span class="line-number">  79</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">final</span> Grid grid;
<span class="line-number">  80</span>     <span class="keyword-directive">private</span> CommandHistory cmdHist;
<span class="line-number">  81</span>     <span class="keyword-directive">private</span> Simulator simulator;
<span class="line-number">  82</span>     <span class="keyword-directive">private</span> Circuit logicalCircuit;
<span class="line-number">  83</span>     <span class="keyword-directive">private</span> ViewerFrame loggerWindow;
<span class="line-number">  84</span>     <span class="keyword-directive">private</span> SimulatorState simulatorState = SimulatorState.STOPPED;
<span class="line-number">  85</span>     <span class="keyword-directive">private</span> Properties properties;
<span class="line-number">  86</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">boolean</span> isSubcircuit;
<span class="line-number">  87</span>     <span class="keyword-directive">private</span> CircuitPanel subcircuitParent;
<span class="line-number">  88</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">int</span> simulationRate;
<span class="line-number">  89</span> 
<span class="line-number">  90</span>     <span class="keyword-directive">public</span> CircuitPanel(CircuitFrame parentFrame, <span class="keyword-directive">boolean</span> isSubCircuit){
<span class="line-number">  91</span>         addMouseMotionListener(<span class="keyword-directive">new</span> CircuitPanelMouseMotionAdapter());
<span class="line-number">  92</span>         addMouseListener(<span class="keyword-directive">new</span> CircuitPanelMouseAdapter());
<span class="line-number">  93</span>         <span class="keyword-directive">this</span>.logicalCircuit = <span class="keyword-directive">new</span> Circuit();
<span class="line-number">  94</span>         <span class="keyword-directive">this</span>.loggerWindow = <span class="keyword-directive">new</span> ViewerFrame(<span class="keyword-directive">this</span>);
<span class="line-number">  95</span>         <span class="keyword-directive">this</span>.loggerWindow.setVisible(<span class="keyword-directive">false</span>);
<span class="line-number">  96</span>         <span class="keyword-directive">this</span>.simulator = <span class="keyword-directive">new</span> Simulator(logicalCircuit);
<span class="line-number">  97</span>         <span class="keyword-directive">this</span>.simulator.addStateListener(<span class="keyword-directive">this</span>);
<span class="line-number">  98</span>         <span class="keyword-directive">this</span>.simulator.addStateListener(loggerWindow.getSimStateListener());
<span class="line-number">  99</span>         <span class="keyword-directive">this</span>.parentFrame = parentFrame;
<span class="line-number"> 100</span>         <span class="keyword-directive">this</span>.editor = ((CircuitFrame) getParentFrame()).getEditor();
<span class="line-number"> 101</span>         <span class="keyword-directive">this</span>.cmdHist = <span class="keyword-directive">new</span> CommandHistory(editor);
<span class="line-number"> 102</span>         <span class="keyword-directive">this</span>.grid = <span class="keyword-directive">new</span> Grid(<span class="keyword-directive">this</span>);
<span class="line-number"> 103</span>         <span class="keyword-directive">this</span>.simulationRate = simulator.getSimulatorSpeed();
<span class="line-number"> 104</span>         <span class="keyword-directive">this</span>.isSubcircuit = isSubCircuit;
<span class="line-number"> 105</span>         createDefaultProperties();        
<span class="line-number"> 106</span>         <span class="keyword-directive">if</span>(isSubcircuit){
<span class="line-number"> 107</span>             setBackground(UIConstants.SUBCIRCUIT_BACKGROUND_COLOUR);
<span class="line-number"> 108</span>         } <span class="keyword-directive">else</span> {
<span class="line-number"> 109</span>             setBackground(UIConstants.CIRCUIT_BACKGROUND_COLOUR);
<span class="line-number"> 110</span>         }
<span class="line-number"> 111</span>         setPreferredSize(<span class="keyword-directive">new</span> Dimension(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().getSize()));
<span class="line-number"> 112</span>         setMaximumSize(<span class="keyword-directive">new</span> Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
<span class="line-number"> 113</span>     }
<span class="line-number"> 114</span> 
<span class="line-number"> 115</span>     <span class="comment">/**</span>
<span class="line-number"> 116</span> <span class="comment">     * </span><span class="comment">Remove</span> <span class="comment">any</span> <span class="comment">components</span> <span class="comment">that</span> <span class="comment">are</span> <span class="comment">still</span> <span class="comment">floating</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">workarea</span> <span class="comment">and</span> <span class="comment">have</span> <span class="comment">not</span> <span class="comment">been</span> <span class="comment">fixed</span><span class="comment">.</span>
<span class="line-number"> 117</span>      <span class="comment">*/</span>
<span class="line-number"> 118</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> removeUnfixedComponents() {
<span class="line-number"> 119</span>         loggerWindow.clearPinLoggers();
<span class="line-number"> 120</span>         LinkedList&lt;SelectableComponent&gt; fixedComponents = <span class="keyword-directive">new</span> LinkedList&lt;SelectableComponent&gt;();
<span class="line-number"> 121</span>         <span class="keyword-directive">for</span>(SelectableComponent sc: drawnComponents){
<span class="line-number"> 122</span>             <span class="keyword-directive">if</span>(sc.isFixed() &amp;&amp; sc.getParent().equals(<span class="keyword-directive">this</span>)){
<span class="line-number"> 123</span>                 <span class="keyword-directive">if</span>(sc <span class="keyword-directive">instanceof</span> PinLogger){
<span class="line-number"> 124</span>                     loggerWindow.addPinLogger((PinLogger)sc);
<span class="line-number"> 125</span>                 }                
<span class="line-number"> 126</span>                 fixedComponents.push(sc);
<span class="line-number"> 127</span>             } <span class="keyword-directive">else</span> {
<span class="line-number"> 128</span>                 grid.removeComponent(sc);
<span class="line-number"> 129</span>                 logicalCircuit.removeSimItem(sc.getLogicalComponent());
<span class="line-number"> 130</span>             }
<span class="line-number"> 131</span>         }
<span class="line-number"> 132</span>         drawnComponents.clear();
<span class="line-number"> 133</span>         drawnComponents.addAll(fixedComponents);
<span class="line-number"> 134</span>         repaint();
<span class="line-number"> 135</span>     }
<span class="line-number"> 136</span>     
<span class="line-number"> 137</span>     <span class="comment">/**</span>
<span class="line-number"> 138</span> <span class="comment">     * </span><span class="comment">Change</span> <span class="comment">the</span> <span class="comment">z</span><span class="comment">-</span><span class="comment">order</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">component</span> <span class="comment">and</span> <span class="comment">bring</span> <span class="comment">it</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">front</span><span class="comment">.</span>
<span class="line-number"> 139</span> <span class="comment">     * </span><span class="ST0">@param</span> <span class="comment">sc</span> <span class="comment">The</span> <span class="comment">component</span> <span class="comment">to</span> <span class="comment">change</span><span class="comment">.</span>
<span class="line-number"> 140</span>      <span class="comment">*/</span>
<span class="line-number"> 141</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> bringComponentToFront(SelectableComponent sc) {
<span class="line-number"> 142</span>         <span class="keyword-directive">if</span>(drawnComponents.remove(sc)){
<span class="line-number"> 143</span>             drawnComponents.add(sc);
<span class="line-number"> 144</span>         }
<span class="line-number"> 145</span>     }
<span class="line-number"> 146</span> 
<span class="line-number"> 147</span>     <span class="comment">/**</span>
<span class="line-number"> 148</span> <span class="comment">     * </span><span class="comment">Unselect</span> <span class="comment">active</span> <span class="comment">selection</span> <span class="comment">of</span> <span class="comment">components</span><span class="comment">.</span> <span class="comment">Also</span> <span class="comment">tell</span> <span class="comment">the</span> <span class="comment">editor</span> <span class="comment">so</span> <span class="comment">that</span> <span class="comment">it</span> <span class="comment">can</span> 
<span class="line-number"> 149</span> <span class="comment">     * </span><span class="comment">disable</span> <span class="comment">any</span> <span class="comment">actions</span> <span class="comment">that</span> <span class="comment">require</span> <span class="comment">an</span> <span class="comment">active</span> <span class="comment">selection</span><span class="comment">.</span>
<span class="line-number"> 150</span>      <span class="comment">*/</span>
<span class="line-number"> 151</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> resetActiveComponents() {
<span class="line-number"> 152</span>         <span class="keyword-directive">for</span> (SelectableComponent sc : activeComponents) {
<span class="line-number"> 153</span>             sc.resetDefaultState();
<span class="line-number"> 154</span>         }
<span class="line-number"> 155</span>         activeComponents.clear();
<span class="line-number"> 156</span>         editor.getClipboard().setHasSelection(<span class="keyword-directive">false</span>);
<span class="line-number"> 157</span>     }
<span class="line-number"> 158</span>     
<span class="line-number"> 159</span>     <span class="comment">/**</span>
<span class="line-number"> 160</span> <span class="comment">     * </span><span class="comment">Make</span> <span class="comment">every</span> <span class="comment">component</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">circuit</span> <span class="comment">active</span><span class="comment">.</span> <span class="comment">Also</span> <span class="comment">tell</span> <span class="comment">the</span> <span class="comment">editor</span> <span class="comment">so</span> <span class="comment">that</span> <span class="comment">it</span> <span class="comment">can</span> 
<span class="line-number"> 161</span> <span class="comment">     * </span><span class="comment">enable</span> <span class="comment">any</span> <span class="comment">actions</span> <span class="comment">that</span> <span class="comment">require</span> <span class="comment">an</span> <span class="comment">active</span> <span class="comment">selection</span><span class="comment">.</span>
<span class="line-number"> 162</span>      <span class="comment">*/</span>
<span class="line-number"> 163</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> selectAllComponents() {
<span class="line-number"> 164</span>         activeComponents.clear();
<span class="line-number"> 165</span>         <span class="keyword-directive">for</span> (SelectableComponent sc : drawnComponents) {
<span class="line-number"> 166</span>             sc.setSelectionState(SelectionState.ACTIVE);
<span class="line-number"> 167</span>             activeComponents.add(sc);
<span class="line-number"> 168</span>         }
<span class="line-number"> 169</span>         editor.getClipboard().setHasSelection(<span class="keyword-directive">true</span>);
<span class="line-number"> 170</span>         repaint();
<span class="line-number"> 171</span>     }
<span class="line-number"> 172</span>     
<span class="line-number"> 173</span>     <span class="comment">/**</span> <span class="ST0">@return</span> <span class="comment">Does</span> <span class="comment">this</span> <span class="comment">circuit</span> <span class="comment">has</span> <span class="comment">any</span> <span class="comment">components</span> <span class="comment">selected</span><span class="comment">? </span><span class="comment">*/</span>
<span class="line-number"> 174</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">boolean</span> hasActiveSelection(){
<span class="line-number"> 175</span>         <span class="keyword-directive">for</span>(SelectableComponent sc: drawnComponents){
<span class="line-number"> 176</span>             <span class="keyword-directive">if</span>(sc.getSelectionState().equals(SelectionState.ACTIVE)){
<span class="line-number"> 177</span>                 <span class="keyword-directive">return</span> <span class="keyword-directive">true</span>;
<span class="line-number"> 178</span>             }
<span class="line-number"> 179</span>         }        
<span class="line-number"> 180</span>         <span class="keyword-directive">return</span> <span class="keyword-directive">false</span>;
<span class="line-number"> 181</span>     }
<span class="line-number"> 182</span>     
<span class="line-number"> 183</span>     <span class="comment">/**</span> <span class="comment">Delete</span> <span class="comment">the</span> <span class="comment">current</span> <span class="comment">selection</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">circuit</span><span class="comment">.</span> <span class="comment">*/</span>
<span class="line-number"> 184</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> deleteActiveComponents(){        
<span class="line-number"> 185</span>         <span class="keyword-directive">for</span>(SelectableComponent sc: activeComponents){
<span class="line-number"> 186</span>             grid.removeComponent(sc);
<span class="line-number"> 187</span>             logicalCircuit.removeSimItem(sc.getLogicalComponent());
<span class="line-number"> 188</span>         }        
<span class="line-number"> 189</span>         drawnComponents.removeAll(activeComponents);        
<span class="line-number"> 190</span>         activeComponents.clear();
<span class="line-number"> 191</span>         editor.getClipboard().setHasSelection(<span class="keyword-directive">false</span>);
<span class="line-number"> 192</span>         repaint();
<span class="line-number"> 193</span>     }
<span class="line-number"> 194</span>     
<span class="line-number"> 195</span>     <span class="comment">/**</span> <span class="ST0">@return</span> <span class="comment">The</span> <span class="comment">current</span> <span class="comment">selection</span> <span class="comment">of</span> <span class="comment">active</span> <span class="comment">components</span> <span class="comment">*/</span>
<span class="line-number"> 196</span>     <span class="keyword-directive">public</span> LinkedList&lt;SelectableComponent&gt; getActiveComponents(){
<span class="line-number"> 197</span>         <span class="keyword-directive">return</span> activeComponents;
<span class="line-number"> 198</span>     }
<span class="line-number"> 199</span>     
<span class="line-number"> 200</span>     <span class="comment">/**</span> <span class="ST0">@return</span> <span class="comment">The</span> <span class="comment">current</span> <span class="comment">component</span> <span class="comment">which</span> <span class="comment">has</span> <span class="comment">the</span> <span class="comment">mouse</span> <span class="comment">hovering</span> <span class="comment">over</span> <span class="comment">it</span> <span class="comment">*/</span>
<span class="line-number"> 201</span>     <span class="keyword-directive">private</span> SelectableComponent getHighlightedComponent() {
<span class="line-number"> 202</span>         <span class="keyword-directive">return</span> highlightedComponent;
<span class="line-number"> 203</span>     }
<span class="line-number"> 204</span> 
<span class="line-number"> 205</span>     <span class="comment">/**</span> <span class="ST0">@param</span> <span class="comment">highlightedComponent</span> <span class="comment">The</span> <span class="comment">new</span> <span class="comment">component</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">highlighted</span> <span class="comment">*/</span> 
<span class="line-number"> 206</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">void</span> setHighlightedComponent(SelectableComponent higlightedComponent) {
<span class="line-number"> 207</span>         <span class="keyword-directive">if</span>(highlightedComponent!=<span class="keyword-directive">null</span>){
<span class="line-number"> 208</span>             <span class="keyword-directive">this</span>.highlightedComponent.revertHoverState();
<span class="line-number"> 209</span>         }
<span class="line-number"> 210</span>         <span class="keyword-directive">this</span>.highlightedComponent = higlightedComponent;
<span class="line-number"> 211</span>         <span class="keyword-directive">this</span>.highlightedComponent.setHoverState();
<span class="line-number"> 212</span>     }
<span class="line-number"> 213</span>         
<span class="line-number"> 214</span>     <span class="comment">/**</span>
<span class="line-number"> 215</span> <span class="comment">     * </span><span class="comment">Clear</span> <span class="comment">everything</span> <span class="comment">associated</span> <span class="comment">with</span> <span class="comment">this</span> <span class="comment">circuit</span><span class="comment">.</span>
<span class="line-number"> 216</span>      <span class="comment">*/</span>
<span class="line-number"> 217</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> resetCircuit(){
<span class="line-number"> 218</span>         <span class="keyword-directive">for</span>(SelectableComponent sc:drawnComponents){
<span class="line-number"> 219</span>             <span class="keyword-directive">if</span>(sc <span class="keyword-directive">instanceof</span> PinLogger){
<span class="line-number"> 220</span>                 ((PinLogger) sc).clear();
<span class="line-number"> 221</span>             }
<span class="line-number"> 222</span>         }
<span class="line-number"> 223</span>         drawnComponents.clear();
<span class="line-number"> 224</span>         activeComponents.clear();
<span class="line-number"> 225</span>         highlightedComponent = <span class="keyword-directive">null</span>;
<span class="line-number"> 226</span>         temporaryComponent = <span class="keyword-directive">null</span>;
<span class="line-number"> 227</span>         nowDragingComponent = <span class="keyword-directive">false</span>;
<span class="line-number"> 228</span>         multipleSelection = <span class="keyword-directive">false</span>;
<span class="line-number"> 229</span>         grid.clear();
<span class="line-number"> 230</span>         logicalCircuit.clear();
<span class="line-number"> 231</span>         editor.getClipboard().setHasSelection(<span class="keyword-directive">false</span>);
<span class="line-number"> 232</span>         repaint();      
<span class="line-number"> 233</span>     }    
<span class="line-number"> 234</span>     
<span class="line-number"> 235</span>     <span class="comment">/**</span> <span class="comment">Add</span> <span class="comment">a</span> <span class="comment">list</span> <span class="comment">of</span> <span class="comment">components</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">circuit</span> <span class="comment">*/</span>
<span class="line-number"> 236</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> addComponentList(Collection&lt;SelectableComponent&gt; list){
<span class="line-number"> 237</span>         drawnComponents.addAll(list);
<span class="line-number"> 238</span>         previousCurrentPoint = SelectableComponent.DEFAULT_ORIGIN();
<span class="line-number"> 239</span>         activeComponents.clear();
<span class="line-number"> 240</span>         <span class="keyword-directive">for</span>(SelectableComponent sc: list){
<span class="line-number"> 241</span>             <span class="keyword-directive">if</span>(!sc.isFixed()){
<span class="line-number"> 242</span>                 activeComponents.add(sc);
<span class="line-number"> 243</span>                 nowDragingComponent = <span class="keyword-directive">true</span>;
<span class="line-number"> 244</span>                 lastDragPoint = currentPoint;
<span class="line-number"> 245</span>             }
<span class="line-number"> 246</span>             repaint(sc.getBoundingBox());
<span class="line-number"> 247</span>         }
<span class="line-number"> 248</span>         <span class="keyword-directive">if</span>(nowDragingComponent){dragActiveSelection(<span class="keyword-directive">null</span>, <span class="keyword-directive">false</span>, <span class="keyword-directive">false</span>);}            
<span class="line-number"> 249</span>         editor.getClipboard().setHasSelection(!activeComponents.isEmpty());
<span class="line-number"> 250</span>     }
<span class="line-number"> 251</span>      
<span class="line-number"> 252</span>     <span class="comment">/**</span> <span class="comment">Add</span> <span class="comment">a</span> <span class="comment">single</span> <span class="comment">component</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">circuit</span> <span class="comment">*/</span>
<span class="line-number"> 253</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> addComponent(SelectableComponent sc) {
<span class="line-number"> 254</span>         drawnComponents.push(sc);
<span class="line-number"> 255</span>         previousCurrentPoint = SelectableComponent.DEFAULT_ORIGIN();
<span class="line-number"> 256</span>         setCurrentTool(sc.getKeyName());
<span class="line-number"> 257</span>         repaint(sc.getBoundingBox());
<span class="line-number"> 258</span>     }
<span class="line-number"> 259</span>     
<span class="line-number"> 260</span>     <span class="comment">/**</span> <span class="ST0">@return</span> <span class="comment">Has</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">component</span> <span class="comment">been</span> <span class="comment">drawn</span> <span class="comment">on</span> <span class="comment">this</span> <span class="comment">circuit</span> <span class="comment">*/</span>
<span class="line-number"> 261</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">boolean</span> containsComponent(SelectableComponent sc){
<span class="line-number"> 262</span>         <span class="keyword-directive">return</span> drawnComponents.contains(sc);
<span class="line-number"> 263</span>     }
<span class="line-number"> 264</span>     
<span class="line-number"> 265</span>     <span class="comment">/**</span> <span class="comment">Remove</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">component</span> <span class="comment">from</span> <span class="comment">this</span> <span class="comment">circuit</span> <span class="comment">*/</span>
<span class="line-number"> 266</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> removeComponent(SelectableComponent sc) {
<span class="line-number"> 267</span>         loggerWindow.removePinLogger(sc);
<span class="line-number"> 268</span>         drawnComponents.remove(sc);
<span class="line-number"> 269</span>         grid.removeComponent(sc);
<span class="line-number"> 270</span>         logicalCircuit.removeSimItem(sc.getLogicalComponent());
<span class="line-number"> 271</span>     }
<span class="line-number"> 272</span>     
<span class="line-number"> 273</span>     <span class="comment">/**</span> 
<span class="line-number"> 274</span> <span class="comment">     * </span><span class="comment">Calculate</span> <span class="comment">the</span> <span class="comment">Bounds</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">Selection</span> <span class="comment">box</span> <span class="comment">based</span> <span class="comment">on</span> <span class="comment">mouse</span> <span class="comment">co</span><span class="comment">-</span><span class="comment">ordinates</span> <span class="comment">at</span>
<span class="line-number"> 275</span> <span class="comment">     * </span><span class="comment">certain</span> <span class="comment">mouse</span> <span class="comment">dragging</span> <span class="comment">events</span><span class="comment">.</span>
<span class="line-number"> 276</span>      <span class="comment">*/</span>
<span class="line-number"> 277</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">void</span> setSelectionBox() {
<span class="line-number"> 278</span>         selX = dragStartPoint.x;
<span class="line-number"> 279</span>         selY = dragStartPoint.y;
<span class="line-number"> 280</span>         selWidth = currentPoint.x - dragStartPoint.x;
<span class="line-number"> 281</span>         selHeight = currentPoint.y - dragStartPoint.y;
<span class="line-number"> 282</span> 
<span class="line-number"> 283</span>         <span class="keyword-directive">if</span> (selWidth &lt; 0) {
<span class="line-number"> 284</span>             selX = selX + selWidth;
<span class="line-number"> 285</span>             selWidth = selWidth * -1;
<span class="line-number"> 286</span>         }
<span class="line-number"> 287</span>         <span class="keyword-directive">if</span> (selHeight &lt; 0) {
<span class="line-number"> 288</span>             selY = selY + selHeight;
<span class="line-number"> 289</span>             selHeight = selHeight * -1;
<span class="line-number"> 290</span>         }
<span class="line-number"> 291</span>     }
<span class="line-number"> 292</span>     
<span class="line-number"> 293</span>     <span class="comment">/**</span>
<span class="line-number"> 294</span> <span class="comment">     * </span><span class="ST0">@return</span> <span class="comment">The</span> <span class="comment">connection</span> <span class="comment">point</span> <span class="comment">grid</span> <span class="comment">associated</span> <span class="comment">with</span> <span class="comment">this</span> <span class="comment">Circuit</span><span class="comment">.</span>
<span class="line-number"> 295</span> <span class="comment">     * </span><span class="ST0">@see</span> <span class="comment">ui</span><span class="comment">.</span><span class="comment">grid</span><span class="comment">.</span><span class="comment">Grid</span>
<span class="line-number"> 296</span>      <span class="comment">*/</span>
<span class="line-number"> 297</span>     <span class="keyword-directive">public</span> Grid getGrid() {
<span class="line-number"> 298</span>         <span class="keyword-directive">return</span> grid;
<span class="line-number"> 299</span>     }  
<span class="line-number"> 300</span>         
<span class="line-number"> 301</span>     <span class="comment">/**</span>
<span class="line-number"> 302</span> <span class="comment">     * </span><span class="ST0">@return</span> <span class="comment">The</span> <span class="comment">command</span> <span class="comment">history</span> <span class="comment">of</span> <span class="comment">actions</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">panel</span>
<span class="line-number"> 303</span> <span class="comment">     * </span><span class="ST0">@see</span> <span class="comment">ui</span><span class="comment">.</span><span class="comment">command</span><span class="comment">.</span><span class="comment">CommandHistory</span><span class="comment">;</span>
<span class="line-number"> 304</span>      <span class="comment">*/</span>
<span class="line-number"> 305</span>     <span class="keyword-directive">public</span> CommandHistory getCommandHistory() {
<span class="line-number"> 306</span>         <span class="keyword-directive">return</span> cmdHist;
<span class="line-number"> 307</span>     }
<span class="line-number"> 308</span> 
<span class="line-number"> 309</span>     <span class="comment">/**</span> <span class="comment">Convienience</span> <span class="comment">method</span> <span class="comment">to</span> <span class="comment">execute</span> <span class="comment">a</span> <span class="comment">command</span> <span class="comment">*/</span>
<span class="line-number"> 310</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> doCommand(Command cmd) {
<span class="line-number"> 311</span>         cmdHist.doCommand(cmd);
<span class="line-number"> 312</span>     }
<span class="line-number"> 313</span> 
<span class="line-number"> 314</span>     <span class="comment">/**</span> <span class="comment">The</span> <span class="comment">current</span> <span class="comment">tool</span> <span class="comment">causes</span> <span class="comment">distinctions</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">mouse</span> <span class="comment">actions</span> <span class="comment">*/</span>
<span class="line-number"> 315</span>     <span class="keyword-directive">public</span> String getCurrentTool() {
<span class="line-number"> 316</span>         <span class="keyword-directive">return</span> currentTool;
<span class="line-number"> 317</span>     }
<span class="line-number"> 318</span>     
<span class="line-number"> 319</span>     <span class="comment">/**</span> <span class="ST0">@see</span> <span class="comment">#</span><span class="comment">getCurrentTool</span><span class="comment">() </span><span class="comment">*/</span>
<span class="line-number"> 320</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> setCurrentTool(String tool) {
<span class="line-number"> 321</span>         <span class="keyword-directive">this</span>.currentTool = tool;
<span class="line-number"> 322</span>     }
<span class="line-number"> 323</span> 
<span class="line-number"> 324</span>     <span class="comment">/**</span> <span class="comment">The</span> <span class="comment">Frame</span> <span class="comment">containing</span> <span class="comment">any</span> <span class="comment">logger</span> <span class="comment">graphs</span><span class="comment"> *</span><span class="comment">*/</span>
<span class="line-number"> 325</span>     <span class="keyword-directive">public</span> ViewerFrame getLoggerWindow() {
<span class="line-number"> 326</span>         <span class="keyword-directive">return</span> loggerWindow;
<span class="line-number"> 327</span>     }
<span class="line-number"> 328</span> 
<span class="line-number"> 329</span>     <span class="comment">/**</span> <span class="comment">The</span> <span class="comment">logical</span> <span class="comment">represenation</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">circuit</span> <span class="comment">*/</span>
<span class="line-number"> 330</span>     <span class="keyword-directive">public</span> Circuit getLogicalCircuit() {
<span class="line-number"> 331</span>         <span class="keyword-directive">return</span> logicalCircuit;
<span class="line-number"> 332</span>     }
<span class="line-number"> 333</span> 
<span class="line-number"> 334</span>     <span class="comment">/**</span> <span class="comment">Does</span> <span class="comment">this</span> <span class="comment">panel</span> <span class="comment">contain</span> <span class="comment">a</span> <span class="comment">sub</span> <span class="comment">circuit</span><span class="comment">? </span><span class="comment">*/</span>
<span class="line-number"> 335</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">boolean</span> isSubcircuit() {
<span class="line-number"> 336</span>         <span class="keyword-directive">return</span> isSubcircuit;
<span class="line-number"> 337</span>     }
<span class="line-number"> 338</span>     
<span class="line-number"> 339</span>     <span class="comment">/**</span> <span class="comment">Set</span> <span class="comment">the</span> <span class="comment">parent</span> <span class="comment">which</span> <span class="comment">contains</span> <span class="comment">this</span> <span class="comment">circuit</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">sub</span> <span class="comment">circuit</span> <span class="comment">*/</span>
<span class="line-number"> 340</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> setSubcircuitParent(CircuitPanel parent) {
<span class="line-number"> 341</span>         <span class="keyword-directive">this</span>.subcircuitParent = parent;
<span class="line-number"> 342</span>     }
<span class="line-number"> 343</span>     
<span class="line-number"> 344</span>     <span class="comment">/**</span> <span class="comment">Update</span> <span class="comment">any</span> <span class="comment">subcircuit</span> <span class="comment">that</span> <span class="comment">this</span> <span class="comment">panel</span> <span class="comment">has</span> <span class="comment">to</span> <span class="comment">reflect</span> <span class="comment">changes</span> <span class="comment">made</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">source</span> <span class="comment">*/</span>
<span class="line-number"> 345</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">void</span> updateSubcircuits(CircuitPanel subcircuitPanel) {
<span class="line-number"> 346</span>         <span class="keyword-directive">for</span>(SelectableComponent sc: drawnComponents){
<span class="line-number"> 347</span>             <span class="keyword-directive">if</span>(sc <span class="keyword-directive">instanceof</span> SubcircuitComponent 
<span class="line-number"> 348</span>                     &amp;&amp; sc.getKeyName().equals(subcircuitPanel.getFilename())){
<span class="line-number"> 349</span>                 ((SubcircuitComponent) sc).updateSource(subcircuitPanel);
<span class="line-number"> 350</span>             }
<span class="line-number"> 351</span>         }
<span class="line-number"> 352</span>         repaint();
<span class="line-number"> 353</span>     }    
<span class="line-number"> 354</span>         
<span class="line-number"> 355</span>     <span class="comment">/**</span> <span class="ST0">@return</span> <span class="comment">The</span> <span class="comment">logical</span> <span class="comment">simulator</span><span class="comment">, </span><span class="comment">responsible</span> <span class="comment">for</span> <span class="comment">changes</span> <span class="comment">in</span> <span class="comment">time</span><span class="comment">, </span><span class="comment">state</span> <span class="comment">and</span> <span class="comment">tiggering</span> <span class="comment">changes</span> <span class="comment">*/</span>
<span class="line-number"> 356</span>     <span class="keyword-directive">public</span> Simulator getSimulator() {
<span class="line-number"> 357</span>         <span class="keyword-directive">return</span> simulator;
<span class="line-number"> 358</span>     }
<span class="line-number"> 359</span> 
<span class="line-number"> 360</span>     <span class="comment">/**</span> <span class="ST0">@return</span> <span class="comment">The</span> <span class="comment">current</span> <span class="comment">state</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">logical</span> <span class="comment">simulator</span><span class="comment">.</span> <span class="comment">*/</span>
<span class="line-number"> 361</span>     <span class="keyword-directive">public</span> SimulatorState getSimulatorState() {
<span class="line-number"> 362</span>         <span class="keyword-directive">return</span> simulatorState;
<span class="line-number"> 363</span>     }
<span class="line-number"> 364</span>     
<span class="line-number"> 365</span>     <span class="comment">/**</span><span class="comment"> {</span><span class="ST0">@inheritDoc</span><span class="comment"> } </span><span class="comment">*/</span>
<span class="line-number"> 366</span>     @Override
<span class="line-number"> 367</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> SimulationTimeChanged(<span class="keyword-directive">long</span> time) {     
<span class="line-number"> 368</span>         <span class="keyword-directive">if</span>(time % Math.pow(10, simulationRate -2 ) == 0  || simulationRate &lt; 2){<span class="comment">// Don&#39;t change too quickly!</span>
<span class="line-number"> 369</span>             cmdHist.stageChange(CommandStage.Message,
<span class="line-number"> 370</span>                     <span class="character">&quot;</span><span class="character">Simulator Time: </span><span class="character">&quot;</span> + ((<span class="keyword-directive">double</span>) (time / (<span class="keyword-directive">double</span>) 1000000000)) + <span class="character">&quot;</span><span class="character">×10</span><span class="ST1">\u2079</span><span class="character">ns</span><span class="character">&quot;</span>);
<span class="line-number"> 371</span>         }
<span class="line-number"> 372</span>     }
<span class="line-number"> 373</span> 
<span class="line-number"> 374</span>     <span class="comment">/**</span><span class="comment"> {</span><span class="ST0">@inheritDoc</span><span class="comment"> } </span><span class="comment">*/</span>
<span class="line-number"> 375</span>     @Override
<span class="line-number"> 376</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> SimulatorStateChanged(SimulatorState state) {
<span class="line-number"> 377</span>         <span class="keyword-directive">this</span>.simulatorState = state;
<span class="line-number"> 378</span>         <span class="keyword-directive">if</span> (getSimulatorState().equals(simulatorState.STOPPED)) {
<span class="line-number"> 379</span>             setBackground(UIConstants.CIRCUIT_BACKGROUND_COLOUR);
<span class="line-number"> 380</span>         } <span class="keyword-directive">else</span> {
<span class="line-number"> 381</span>             <span class="keyword-directive">if</span>(isSubcircuit){
<span class="line-number"> 382</span>                 setBackground(UIConstants.SUBCIRCUIT_BACKGROUND_COLOUR);
<span class="line-number"> 383</span>             } <span class="keyword-directive">else</span> {
<span class="line-number"> 384</span>                 setBackground(UIConstants.CIRCUIT_PLAYING_BACKGROUND_COLOUR);
<span class="line-number"> 385</span>             }
<span class="line-number"> 386</span>         }
<span class="line-number"> 387</span>     }
<span class="line-number"> 388</span>     
<span class="line-number"> 389</span>     <span class="comment">/**</span><span class="comment"> {</span><span class="ST0">@inheritDoc</span><span class="comment"> } </span><span class="comment">*/</span>
<span class="line-number"> 390</span>     @Override
<span class="line-number"> 391</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> SimulationRateChanged(<span class="keyword-directive">int</span> rate){
<span class="line-number"> 392</span>        <span class="keyword-directive">this</span>.simulationRate = rate;
<span class="line-number"> 393</span>     }    
<span class="line-number"> 394</span> 
<span class="line-number"> 395</span>     <span class="comment">/**</span> <span class="comment">Returns</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">and</span> <span class="comment">only</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">circuit</span> <span class="comment">is</span> <span class="comment">currently</span> <span class="comment">active</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">editor</span><span class="comment">.</span> <span class="comment">*/</span>
<span class="line-number"> 396</span>     <span class="keyword-directive">protected</span> <span class="keyword-directive">boolean</span> isActiveCircuit() {
<span class="line-number"> 397</span>         <span class="keyword-directive">return</span> editor.getActiveCircuit().equals(CircuitPanel.<span class="keyword-directive">this</span>);
<span class="line-number"> 398</span>     }
<span class="line-number"> 399</span> 
<span class="line-number"> 400</span>     <span class="comment">/**</span> <span class="ST0">@return</span> <span class="comment">the</span> <span class="comment">JFrame</span> <span class="comment">within</span> <span class="comment">which</span> <span class="comment">this</span> <span class="comment">panel</span> <span class="comment">is</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">workarea</span><span class="comment">*/</span>
<span class="line-number"> 401</span>     <span class="keyword-directive">public</span> CircuitFrame getParentFrame() {
<span class="line-number"> 402</span>         <span class="keyword-directive">return</span> parentFrame;
<span class="line-number"> 403</span>     }
<span class="line-number"> 404</span>     
<span class="line-number"> 405</span>     <span class="comment">/**</span> <span class="ST0">@return</span> <span class="comment">Create</span> <span class="comment">a</span> <span class="comment">filename</span> <span class="comment">if</span> <span class="comment">we</span> <span class="comment">are</span> <span class="comment">untitled</span><span class="comment">, </span><span class="comment">otherwise</span> <span class="comment">return</span> <span class="comment">the</span> <span class="comment">filename</span> <span class="comment">*/</span>
<span class="line-number"> 406</span>     <span class="keyword-directive">public</span> String getFilename() {
<span class="line-number"> 407</span>         <span class="keyword-directive">return</span> (filename == <span class="keyword-directive">null</span>) ? <span class="character">&quot;</span><span class="character">Untitled</span><span class="character">&quot;</span> + getParentFrame().getUntitledIndex() + UIConstants.FILE_EXTENSION : filename;
<span class="line-number"> 408</span>     }
<span class="line-number"> 409</span>     
<span class="line-number"> 410</span>     <span class="comment">/**</span> <span class="comment">Convienience</span> <span class="comment">method</span> <span class="comment">to</span> <span class="comment">change</span> <span class="comment">the</span> <span class="comment">filename</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">panel</span><span class="comment">, </span><span class="comment">it</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">parent</span> <span class="comment">frame</span> 
<span class="line-number"> 411</span> <span class="comment">     * </span><span class="comment">and</span> <span class="comment">the</span> <span class="comment">title</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">editor</span> <span class="comment">*/</span>
<span class="line-number"> 412</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> setFilename(String filename) {
<span class="line-number"> 413</span>         <span class="keyword-directive">this</span>.filename = filename;
<span class="line-number"> 414</span>         <span class="keyword-directive">this</span>.getParentFrame().setTitle(filename);
<span class="line-number"> 415</span>         <span class="keyword-directive">if</span>(!isSubcircuit){
<span class="line-number"> 416</span>             editor.setTitle(<span class="character">&quot;</span><span class="character">Logic Circuit Workbench - </span><span class="character">&quot;</span> + filename);
<span class="line-number"> 417</span>         }
<span class="line-number"> 418</span>     }
<span class="line-number"> 419</span>     
<span class="line-number"> 420</span>     <span class="comment">/**</span> <span class="comment">Save</span> <span class="comment">this</span> <span class="comment">circuit</span> <span class="comment">to</span> <span class="comment">disk</span><span class="comment">, </span><span class="comment">required</span> <span class="comment">to</span> <span class="comment">access</span> <span class="comment">hidden</span> <span class="comment">component</span> <span class="comment">list</span> <span class="comment">*/</span>
<span class="line-number"> 421</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> saveAs(String filename) {
<span class="line-number"> 422</span>         setFilename(filename);
<span class="line-number"> 423</span>         FileCreator.write(<span class="keyword-directive">this</span>, drawnComponents);
<span class="line-number"> 424</span>         CircuitFrame frame = getParentFrame();
<span class="line-number"> 425</span>         frame.setTitle(filename);
<span class="line-number"> 426</span>         cmdHist.setIsDirty(<span class="keyword-directive">false</span>);
<span class="line-number"> 427</span>         
<span class="line-number"> 428</span>         <span class="keyword-directive">if</span>(isSubcircuit()){
<span class="line-number"> 429</span>             subcircuitParent.updateSubcircuits(<span class="keyword-directive">this</span>);
<span class="line-number"> 430</span>         }
<span class="line-number"> 431</span>     }
<span class="line-number"> 432</span>         
<span class="line-number"> 433</span>     <span class="comment">/**</span><span class="comment"> {</span><span class="ST0">@inheritDoc</span><span class="comment">} </span>
<span class="line-number"> 434</span> <span class="comment">     * </span><span class="comment">Also</span> <span class="comment">print</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">correct</span> <span class="comment">area</span> <span class="comment">as</span> <span class="comment">determined</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">double</span> <span class="comment">buffering</span> <span class="comment">test</span><span class="comment">.</span>
<span class="line-number"> 435</span>      <span class="comment">*/</span>
<span class="line-number"> 436</span>     @Override
<span class="line-number"> 437</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> paintComponent(Graphics g) {
<span class="line-number"> 438</span>         <span class="keyword-directive">super</span>.paintComponent(g);
<span class="line-number"> 439</span>         <span class="keyword-directive">if</span> (editor.drawDirect()) {
<span class="line-number"> 440</span>             drawCircuit(g);
<span class="line-number"> 441</span>         } <span class="keyword-directive">else</span> {
<span class="line-number"> 442</span>             Graphics offscreenGraphics = editor.getOffscreenGraphics();
<span class="line-number"> 443</span>             Image offscreenImage = editor.getOffscreenImage();
<span class="line-number"> 444</span>             offscreenGraphics.setColor(getBackground());
<span class="line-number"> 445</span>             offscreenGraphics.fillRect(0, 0, getWidth(), getHeight());
<span class="line-number"> 446</span>             offscreenGraphics.setColor(getForeground());
<span class="line-number"> 447</span> 
<span class="line-number"> 448</span>             <span class="comment">// Paint to the offscreen image</span>
<span class="line-number"> 449</span>             drawCircuit(offscreenGraphics);
<span class="line-number"> 450</span>             <span class="comment">// Copy the offscreen image to the screen</span>
<span class="line-number"> 451</span>             g.drawImage(offscreenImage, 0, 0, <span class="keyword-directive">this</span>);
<span class="line-number"> 452</span>         }
<span class="line-number"> 453</span>     }  
<span class="line-number"> 454</span>     
<span class="line-number"> 455</span>     <span class="comment">/**</span>
<span class="line-number"> 456</span> <span class="comment">     * </span><span class="comment">TODO</span><span class="comment">: </span><span class="comment">javadoc</span>
<span class="line-number"> 457</span>      <span class="comment">*/</span>
<span class="line-number"> 458</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> repaintDirtyAreas() {
<span class="line-number"> 459</span>         Rectangle currentArea = <span class="keyword-directive">new</span> Rectangle(currentPoint);
<span class="line-number"> 460</span>         Rectangle previousArea = <span class="keyword-directive">new</span> Rectangle();
<span class="line-number"> 461</span>         <span class="keyword-directive">int</span> dx = previousCurrentPoint.x - currentPoint.x;
<span class="line-number"> 462</span>         <span class="keyword-directive">int</span> dy = previousCurrentPoint.y - currentPoint.y;
<span class="line-number"> 463</span> 
<span class="line-number"> 464</span>         currentArea.add(previousCurrentPoint);
<span class="line-number"> 465</span>         <span class="keyword-directive">for</span> (SelectableComponent sc : drawnComponents) {
<span class="line-number"> 466</span>             <span class="keyword-directive">if</span> (!sc.isFixed() || sc.equals(highlightedComponent) || activeComponents.contains(sc)) {
<span class="line-number"> 467</span>                 currentArea.add(sc.getBoundingBox());
<span class="line-number"> 468</span>             }
<span class="line-number"> 469</span>         }
<span class="line-number"> 470</span> 
<span class="line-number"> 471</span>         previousArea = currentArea.getBounds();
<span class="line-number"> 472</span>         previousArea.translate(dx, dy);
<span class="line-number"> 473</span>         currentArea.add(previousArea);
<span class="line-number"> 474</span>         currentArea.grow(10, 10);
<span class="line-number"> 475</span> 
<span class="line-number"> 476</span>         repaint(currentArea);
<span class="line-number"> 477</span>     }
<span class="line-number"> 478</span>     
<span class="line-number"> 479</span>     <span class="comment">/**</span> <span class="comment">Paint</span> <span class="comment">areas</span> <span class="comment">within</span> <span class="comment">the</span> <span class="comment">clip</span> <span class="comment">rectangle</span> <span class="comment">onto</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">graphics</span> <span class="comment">context</span> <span class="comment">*/</span>
<span class="line-number"> 480</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> drawCircuit(Graphics g) {
<span class="line-number"> 481</span>         Graphics2D g2 = (Graphics2D) g;
<span class="line-number"> 482</span>         <span class="keyword-directive">if</span> (g2.getClip() == <span class="keyword-directive">null</span>) {
<span class="line-number"> 483</span>             g2.setClip(0, 0, getWidth(), getHeight());
<span class="line-number"> 484</span>         }
<span class="line-number"> 485</span>         
<span class="line-number"> 486</span>         <span class="comment">// Draw the grid dots</span>
<span class="line-number"> 487</span>         <span class="keyword-directive">if</span> (UIConstants.DRAW_GRID_DOTS) {
<span class="line-number"> 488</span>             Point start = Grid.snapToGrid(<span class="keyword-directive">new</span> Point(g2.getClipBounds().x, g2.getClipBounds().y));
<span class="line-number"> 489</span>             g2.setColor(UIConstants.GRID_DOT_COLOUR);
<span class="line-number"> 490</span>             <span class="keyword-directive">for</span> (<span class="keyword-directive">int</span> i = start.x; i &lt; g2.getClipBounds().getMaxX(); i += UIConstants.GRID_DOT_SPACING) {
<span class="line-number"> 491</span>                 <span class="keyword-directive">for</span> (<span class="keyword-directive">int</span> j = start.y; j &lt; g2.getClipBounds().getMaxY(); j += UIConstants.GRID_DOT_SPACING) {
<span class="line-number"> 492</span>                     g2.fillRect(i, j, 1, 1);
<span class="line-number"> 493</span>                 }
<span class="line-number"> 494</span>             }
<span class="line-number"> 495</span>         }
<span class="line-number"> 496</span>         
<span class="line-number"> 497</span>         <span class="comment">// Draw the components</span>
<span class="line-number"> 498</span>         <span class="keyword-directive">for</span> (SelectableComponent sc : drawnComponents) {
<span class="line-number"> 499</span>             <span class="keyword-directive">if</span> (sc.getBoundingBox().intersects(g2.getClipBounds())) {
<span class="line-number"> 500</span>                 <span class="comment">//g2.translate(-sc.getCentre().x, -sc.getCentre().y);</span>
<span class="line-number"> 501</span>                 sc.draw(g2);
<span class="line-number"> 502</span>                 <span class="keyword-directive">if</span> (UIConstants.SHOW_INVALID_AREA_BOXES) {
<span class="line-number"> 503</span>                     g2.draw(sc.getInvalidArea());
<span class="line-number"> 504</span>                 }
<span class="line-number"> 505</span>                 <span class="keyword-directive">if</span> (UIConstants.SHOW_BOUNDING_BOXES) {
<span class="line-number"> 506</span>                     g2.draw(sc.getBoundingBox());
<span class="line-number"> 507</span>                 }
<span class="line-number"> 508</span>                 <span class="comment">//g2.translate(sc.getCentre().x, sc.getCentre().y);                </span>
<span class="line-number"> 509</span>             }
<span class="line-number"> 510</span>         }
<span class="line-number"> 511</span> 
<span class="line-number"> 512</span>         <span class="comment">// Draw Connection Points</span>
<span class="line-number"> 513</span>         grid.draw(g2);
<span class="line-number"> 514</span> 
<span class="line-number"> 515</span>         <span class="comment">// Draw Selection Box</span>
<span class="line-number"> 516</span>         <span class="keyword-directive">if</span> (multipleSelection) {
<span class="line-number"> 517</span>             g2.setColor(UIConstants.SELECTION_BOX_COLOUR);
<span class="line-number"> 518</span>             g2.setStroke(UIConstants.SELECTION_BOX_STROKE);
<span class="line-number"> 519</span>             g2.drawRect(selX, selY, selWidth, selHeight);
<span class="line-number"> 520</span>         }
<span class="line-number"> 521</span>     }
<span class="line-number"> 522</span>     
<span class="line-number"> 523</span>     <span class="comment">/**</span> <span class="comment">Draw</span> <span class="comment">the</span> <span class="comment">circuit</span> <span class="comment">to</span> <span class="comment">a</span> <span class="comment">buffered</span> <span class="comment">image</span> <span class="comment">and</span> <span class="comment">write</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">filename</span> <span class="comment">specified</span><span class="comment">.</span> <span class="comment">*/</span>
<span class="line-number"> 524</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> createImage(String filename) {
<span class="line-number"> 525</span>         BufferedImage bi = <span class="keyword-directive">new</span> BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
<span class="line-number"> 526</span>         paint(bi.getGraphics());
<span class="line-number"> 527</span>         <span class="keyword-directive">try</span> {
<span class="line-number"> 528</span>             ImageIO.write(bi, <span class="character">&quot;</span><span class="character">png</span><span class="character">&quot;</span>, <span class="keyword-directive">new</span> File(filename + <span class="character">&quot;</span><span class="character">.png</span><span class="character">&quot;</span>));
<span class="line-number"> 529</span>         } <span class="keyword-directive">catch</span> (IOException ex) {
<span class="line-number"> 530</span>             ErrorHandler.newError(<span class="character">&quot;</span><span class="character">Image Creation Error</span><span class="character">&quot;</span>, <span class="character">&quot;</span><span class="character">Please refer to the system output below.</span><span class="character">&quot;</span>, ex);
<span class="line-number"> 531</span>         }
<span class="line-number"> 532</span>     }
<span class="line-number"> 533</span> 
<span class="line-number"> 534</span>     <span class="comment">/**</span> <span class="comment">Create</span> <span class="comment">a</span> <span class="comment">standard</span> <span class="comment">set</span> <span class="comment">of</span> <span class="comment">properties</span> <span class="comment">for</span> <span class="comment">this</span> <span class="comment">circuit</span> <span class="comment">if</span> <span class="comment">it</span> <span class="comment">was</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">used</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">subcircuit</span> <span class="comment">*/</span>
<span class="line-number"> 535</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">void</span> createDefaultProperties() {
<span class="line-number"> 536</span>         <span class="keyword-directive">this</span>.properties = <span class="keyword-directive">new</span> Properties(getFilename()){
<span class="line-number"> 537</span>             {
<span class="line-number"> 538</span>                 <span class="comment">// Default logical description</span>
<span class="line-number"> 539</span>                 setLogicalComponentClass(logicalCircuit.getClass());      
<span class="line-number"> 540</span>                 
<span class="line-number"> 541</span>                 <span class="comment">// Circuit Attributes</span>
<span class="line-number"> 542</span>                 addAttribute(<span class="keyword-directive">new</span> TextAttribute(<span class="character">&quot;</span><span class="character">Title</span><span class="character">&quot;</span>, <span class="character">&quot;&quot;</span>));
<span class="line-number"> 543</span>                 addAttribute(<span class="keyword-directive">new</span> ButtonAttribute(<span class="character">&quot;</span><span class="character">Description</span><span class="character">&quot;</span>) {
<span class="line-number"> 544</span>                     @Override
<span class="line-number"> 545</span>                     <span class="keyword-directive">protected</span> <span class="keyword-directive">void</span> buttonClickAction(ActionEvent e) {
<span class="line-number"> 546</span>                         JOptionPane.showInputDialog(jcomponent, getValue(), <span class="character">&quot;</span><span class="character">Description</span><span class="character">&quot;</span>, JOptionPane.INFORMATION_MESSAGE);
<span class="line-number"> 547</span>                     }
<span class="line-number"> 548</span>                 });
<span class="line-number"> 549</span>                 addAttribute(<span class="keyword-directive">new</span> ButtonAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Image</span><span class="character">&quot;</span>) {
<span class="line-number"> 550</span>                     @Override
<span class="line-number"> 551</span>                     <span class="keyword-directive">protected</span> <span class="keyword-directive">void</span> buttonClickAction(ActionEvent e) {
<span class="line-number"> 552</span>                         JFileChooser c = <span class="keyword-directive">new</span> JFileChooser();
<span class="line-number"> 553</span>                         FileFilter jpgFilter = <span class="keyword-directive">new</span> JPGFileFilter();        
<span class="line-number"> 554</span>                         c.setFileFilter(jpgFilter);
<span class="line-number"> 555</span>                         c.setDialogType(JFileChooser.SAVE_DIALOG);
<span class="line-number"> 556</span>                         c.setSelectedFile(<span class="keyword-directive">new</span> java.io.File(filename));
<span class="line-number"> 557</span>                         <span class="keyword-directive">int</span> rVal = c.showOpenDialog(jcomponent);
<span class="line-number"> 558</span>                         <span class="keyword-directive">if</span> (rVal == JFileChooser.APPROVE_OPTION) {
<span class="line-number"> 559</span>                             changeValue(c.getSelectedFile().getAbsolutePath());
<span class="line-number"> 560</span>                         }
<span class="line-number"> 561</span>                     }
<span class="line-number"> 562</span>                 });
<span class="line-number"> 563</span>                 addAttribute(<span class="keyword-directive">new</span> SpinnerAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Width</span><span class="character">&quot;</span>, 40, 10, Short.MAX_VALUE, UIConstants.GRID_DOT_SPACING));
<span class="line-number"> 564</span>                 addAttribute(<span class="keyword-directive">new</span> SpinnerAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Height</span><span class="character">&quot;</span>, 40, 10, Short.MAX_VALUE, UIConstants.GRID_DOT_SPACING));
<span class="line-number"> 565</span>                 
<span class="line-number"> 566</span>                 <span class="comment">// Subcircuit Image?</span>
<span class="line-number"> 567</span>                 <span class="keyword-directive">if</span>(!getAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Image</span><span class="character">&quot;</span>).getValue().equals(<span class="character">&quot;&quot;</span>)){
<span class="line-number"> 568</span>                     File imagefile = <span class="keyword-directive">new</span> File((String)getAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Image</span><span class="character">&quot;</span>).getValue());
<span class="line-number"> 569</span>                     <span class="keyword-directive">if</span>(imagefile.exists()){
<span class="line-number"> 570</span>                         addImage(getFilename()+<span class="character">&quot;</span><span class="character">.default</span><span class="character">&quot;</span>, (String)properties.getAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Image</span><span class="character">&quot;</span>).getValue());
<span class="line-number"> 571</span>                     } 
<span class="line-number"> 572</span>                 }
<span class="line-number"> 573</span>             }          
<span class="line-number"> 574</span>         };
<span class="line-number"> 575</span>     }
<span class="line-number"> 576</span>     
<span class="line-number"> 577</span>     <span class="comment">/**</span> <span class="comment">Refresh</span> <span class="comment">the</span> <span class="comment">subcircuit</span> <span class="comment">properties</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">panel</span> <span class="comment">to</span> <span class="comment">reflect</span> <span class="comment">any</span> <span class="comment">changes</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">circuit</span> <span class="comment">contents</span><span class="comment">.</span> <span class="comment">*/</span>
<span class="line-number"> 578</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> updateProperties(){
<span class="line-number"> 579</span>          Properties newproperties = <span class="keyword-directive">new</span> Properties(getFilename()){
<span class="line-number"> 580</span>              {
<span class="line-number"> 581</span>                 <span class="comment">// Default logical description</span>
<span class="line-number"> 582</span>                 setLogicalComponentClass(properties.getLogicalComponentClass());
<span class="line-number"> 583</span>                 
<span class="line-number"> 584</span>                 <span class="comment">// Circuit Attributes</span>
<span class="line-number"> 585</span>                 addAttribute(properties.getAttribute(<span class="character">&quot;</span><span class="character">Title</span><span class="character">&quot;</span>));
<span class="line-number"> 586</span>                 addAttribute(properties.getAttribute(<span class="character">&quot;</span><span class="character">Description</span><span class="character">&quot;</span>));
<span class="line-number"> 587</span>                 addAttribute(properties.getAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Image</span><span class="character">&quot;</span>));
<span class="line-number"> 588</span>                 addAttribute(properties.getAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Width</span><span class="character">&quot;</span>));
<span class="line-number"> 589</span>                 addAttribute(properties.getAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Height</span><span class="character">&quot;</span>));
<span class="line-number"> 590</span>                 
<span class="line-number"> 591</span>                 <span class="comment">// Subcircuit Image?</span>
<span class="line-number"> 592</span>                 <span class="keyword-directive">if</span>(!getAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Image</span><span class="character">&quot;</span>).getValue().equals(<span class="character">&quot;&quot;</span>)){
<span class="line-number"> 593</span>                     File imagefile = <span class="keyword-directive">new</span> File((String)getAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Image</span><span class="character">&quot;</span>).getValue());
<span class="line-number"> 594</span>                     <span class="keyword-directive">if</span>(imagefile.exists()){
<span class="line-number"> 595</span>                         addImage(getFilename()+<span class="character">&quot;</span><span class="character">.default</span><span class="character">&quot;</span>, (String)properties.getAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Image</span><span class="character">&quot;</span>).getValue());
<span class="line-number"> 596</span>                     } <span class="keyword-directive">else</span> {
<span class="line-number"> 597</span>                         ErrorHandler.newError(<span class="keyword-directive">new</span> ui.error.Error(<span class="character">&quot;</span><span class="character">Initialisation Error</span><span class="character">&quot;</span>,
<span class="line-number"> 598</span>                          <span class="character">&quot;</span><span class="character">Could not find image with key : </span><span class="ST1">\n</span><span class="character">&quot;</span> + (String)getAttribute(<span class="character">&quot;</span><span class="character">Subcircuit Image</span><span class="character">&quot;</span>).getValue())); 
<span class="line-number"> 599</span>                     }
<span class="line-number"> 600</span>                 }
<span class="line-number"> 601</span>                 
<span class="line-number"> 602</span>                 <span class="comment">// Create external pins</span>
<span class="line-number"> 603</span>                 <span class="keyword-directive">int</span> i = 0, o = 0;
<span class="line-number"> 604</span>                 <span class="keyword-directive">for</span>(SelectableComponent sc: drawnComponents){                    
<span class="line-number"> 605</span>                     <span class="comment">// Input Pins</span>
<span class="line-number"> 606</span>                     <span class="keyword-directive">if</span>(sc.isFixed() 
<span class="line-number"> 607</span>                             &amp;&amp; sc.getLogicalComponent() <span class="keyword-directive">instanceof</span> sim.componentLibrary.standard.Input){
<span class="line-number"> 608</span>                         <span class="keyword-directive">int</span> pos = (Integer) sc.getProperties().getAttribute(<span class="character">&quot;</span><span class="character">External Position</span><span class="character">&quot;</span>).getValue();
<span class="line-number"> 609</span>                         String edge = (String) sc.getProperties().getAttribute(<span class="character">&quot;</span><span class="character">External Edge</span><span class="character">&quot;</span>).getValue();
<span class="line-number"> 610</span>                         String label;
<span class="line-number"> 611</span>                         <span class="keyword-directive">if</span>(sc.hasLabel()){
<span class="line-number"> 612</span>                             label = sc.getLabel();
<span class="line-number"> 613</span>                         } <span class="keyword-directive">else</span> {
<span class="line-number"> 614</span>                             label = <span class="character">&quot;</span><span class="character">Input </span><span class="character">&quot;</span> + i;
<span class="line-number"> 615</span>                             sc.setLabel(label);
<span class="line-number"> 616</span>                             i++;
<span class="line-number"> 617</span>                         }
<span class="line-number"> 618</span>                         addInputPin(label, ComponentEdge.convertStringToEdge(edge), pos);                        
<span class="line-number"> 619</span>                     <span class="comment">// Output Pins    </span>
<span class="line-number"> 620</span>                     } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span>(sc.isFixed() 
<span class="line-number"> 621</span>                             &amp;&amp; sc.getLogicalComponent() <span class="keyword-directive">instanceof</span> sim.componentLibrary.standard.Output){
<span class="line-number"> 622</span>                         <span class="keyword-directive">int</span> pos = (Integer) sc.getProperties().getAttribute(<span class="character">&quot;</span><span class="character">External Position</span><span class="character">&quot;</span>).getValue();
<span class="line-number"> 623</span>                         String edge = (String) sc.getProperties().getAttribute(<span class="character">&quot;</span><span class="character">External Edge</span><span class="character">&quot;</span>).getValue();
<span class="line-number"> 624</span>                         String label;
<span class="line-number"> 625</span>                         <span class="keyword-directive">if</span>(sc.hasLabel()){
<span class="line-number"> 626</span>                             label = sc.getLabel();
<span class="line-number"> 627</span>                         } <span class="keyword-directive">else</span> {
<span class="line-number"> 628</span>                             label = <span class="character">&quot;</span><span class="character">Output </span><span class="character">&quot;</span> + o;
<span class="line-number"> 629</span>                             sc.setLabel(label);
<span class="line-number"> 630</span>                             o++;
<span class="line-number"> 631</span>                         }
<span class="line-number"> 632</span>                         addOutputPin(label, ComponentEdge.convertStringToEdge(edge), pos);       
<span class="line-number"> 633</span>                     }              
<span class="line-number"> 634</span>                 }
<span class="line-number"> 635</span>              }
<span class="line-number"> 636</span>          };
<span class="line-number"> 637</span>          <span class="keyword-directive">this</span>.properties = newproperties;
<span class="line-number"> 638</span>     }
<span class="line-number"> 639</span>         
<span class="line-number"> 640</span>     @Override
<span class="line-number"> 641</span>     <span class="keyword-directive">public</span> Properties getProperties(){
<span class="line-number"> 642</span>         <span class="keyword-directive">return</span> properties;
<span class="line-number"> 643</span>     }
<span class="line-number"> 644</span>     
<span class="line-number"> 645</span>     @Override
<span class="line-number"> 646</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> setProperties(Properties properties){
<span class="line-number"> 647</span>         <span class="keyword-directive">this</span>.properties = properties;
<span class="line-number"> 648</span>     }
<span class="line-number"> 649</span>     
<span class="line-number"> 650</span>     @Override
<span class="line-number"> 651</span>     <span class="keyword-directive">public</span> String getKeyName(){
<span class="line-number"> 652</span>         <span class="keyword-directive">return</span> <span class="character">&quot;</span><span class="character">Circuit Panel</span><span class="character">&quot;</span>;
<span class="line-number"> 653</span>     }
<span class="line-number"> 654</span> 
<span class="line-number"> 655</span>      <span class="comment">/**</span> <span class="comment">This</span> <span class="comment">inner</span> <span class="comment">class</span> <span class="comment">handles</span> <span class="comment">all</span> <span class="comment">MouseListener</span> <span class="comment">events</span> <span class="comment">*/</span>
<span class="line-number"> 656</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">class</span> CircuitPanelMouseAdapter <span class="keyword-directive">extends</span> MouseAdapter {
<span class="line-number"> 657</span> 
<span class="line-number"> 658</span>         <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> mouseClicked(MouseEvent e) {
<span class="line-number"> 659</span>             <span class="keyword-directive">if</span> (isActiveCircuit() &amp;&amp; !currentTool.equals(<span class="character">&quot;</span><span class="character">Wire</span><span class="character">&quot;</span>)) {
<span class="line-number"> 660</span>                 <span class="comment">// Area we clicking empty space?</span>
<span class="line-number"> 661</span>                 <span class="keyword-directive">boolean</span> clickingEmptySpace = <span class="keyword-directive">true</span>;
<span class="line-number"> 662</span>                 temporaryComponent = <span class="keyword-directive">null</span>;
<span class="line-number"> 663</span>                 <span class="keyword-directive">for</span> (SelectableComponent sc : drawnComponents) {
<span class="line-number"> 664</span>                     <span class="keyword-directive">if</span> (sc.isFixed() &amp;&amp; sc.containsPoint(lastDragPoint)) {
<span class="line-number"> 665</span>                         clickingEmptySpace = <span class="keyword-directive">false</span>;
<span class="line-number"> 666</span>                         temporaryComponent = sc;
<span class="line-number"> 667</span>                         <span class="keyword-directive">break</span>;
<span class="line-number"> 668</span>                     }
<span class="line-number"> 669</span>                 }
<span class="line-number"> 670</span> 
<span class="line-number"> 671</span>                 <span class="keyword-directive">if</span> (clickingEmptySpace) {                    
<span class="line-number"> 672</span>                     resetActiveComponents();
<span class="line-number"> 673</span> 
<span class="line-number"> 674</span>                     <span class="comment">// Fix floating selection</span>
<span class="line-number"> 675</span>                     <span class="keyword-directive">if</span> (!drawnComponents.isEmpty() &amp;&amp; !drawnComponents.peek().isFixed()) {
<span class="line-number"> 676</span>                         SelectableComponent top = drawnComponents.peek();
<span class="line-number"> 677</span>                         editor.fixComponent(top);                        
<span class="line-number"> 678</span>                         <span class="keyword-directive">if</span> (!currentTool.equals(<span class="character">&quot;</span><span class="character">Select</span><span class="character">&quot;</span>)) {
<span class="line-number"> 679</span>                             <span class="comment">// Add another new component                            </span>
<span class="line-number"> 680</span>                             CreateComponentCommand ccc = <span class="keyword-directive">new</span> CreateComponentCommand(
<span class="line-number"> 681</span>                                     CircuitPanel.<span class="keyword-directive">this</span>,
<span class="line-number"> 682</span>                                     currentTool,
<span class="line-number"> 683</span>                                     editor.getComponentRotation(),
<span class="line-number"> 684</span>                                     SelectableComponent.DEFAULT_ORIGIN());
<span class="line-number"> 685</span>                             cmdHist.doCommand(ccc);
<span class="line-number"> 686</span>                             ((VisualComponent)ccc.getComponent()).addLogicalComponentToCircuit();
<span class="line-number"> 687</span> 
<span class="line-number"> 688</span>                             <span class="comment">// To redraw the new component at workarea origin</span>
<span class="line-number"> 689</span>                             previousCurrentPoint = SelectableComponent.DEFAULT_ORIGIN();
<span class="line-number"> 690</span>                         }
<span class="line-number"> 691</span>                     }
<span class="line-number"> 692</span>                 } <span class="keyword-directive">else</span> {
<span class="line-number"> 693</span>                     <span class="comment">// Single Click</span>
<span class="line-number"> 694</span>                     <span class="keyword-directive">if</span>(e.getClickCount() != 2){
<span class="line-number"> 695</span>                         <span class="comment">// Activate selected component</span>
<span class="line-number"> 696</span>                         temporaryComponent.mouseClicked(e);
<span class="line-number"> 697</span>                         <span class="keyword-directive">if</span>(simulatorState.equals(SimulatorState.STOPPED)){
<span class="line-number"> 698</span>                             resetActiveComponents();
<span class="line-number"> 699</span>                             activeComponents.add(temporaryComponent);
<span class="line-number"> 700</span>                             editor.getClipboard().setHasSelection(<span class="keyword-directive">true</span>);                            
<span class="line-number"> 701</span>                         }                        
<span class="line-number"> 702</span>                         repaint(temporaryComponent.getBoundingBox());
<span class="line-number"> 703</span> 
<span class="line-number"> 704</span>                         <span class="comment">// Update the current selection options panel</span>
<span class="line-number"> 705</span>                         editor.setComponent(temporaryComponent);
<span class="line-number"> 706</span>                     <span class="comment">// Double Click</span>
<span class="line-number"> 707</span>                     } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span>(e.getClickCount() == 2 
<span class="line-number"> 708</span>                             &amp;&amp; temporaryComponent <span class="keyword-directive">instanceof</span> SubcircuitComponent){
<span class="line-number"> 709</span>                         ((SubcircuitComponent) temporaryComponent).openEditor();
<span class="line-number"> 710</span>                     }
<span class="line-number"> 711</span>                 }
<span class="line-number"> 712</span>             } 
<span class="line-number"> 713</span>         }
<span class="line-number"> 714</span> 
<span class="line-number"> 715</span>         <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> mousePressed(MouseEvent e) {
<span class="line-number"> 716</span>             <span class="keyword-directive">if</span> (isActiveCircuit()) {
<span class="line-number"> 717</span>                 dragStartPoint = Grid.snapToGrid(e.getPoint());
<span class="line-number"> 718</span>                 lastDragPoint = Grid.snapToGrid(e.getPoint());
<span class="line-number"> 719</span>             }
<span class="line-number"> 720</span>         }
<span class="line-number"> 721</span> 
<span class="line-number"> 722</span>         <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> mouseReleased(MouseEvent e) {            
<span class="line-number"> 723</span>             <span class="keyword-directive">if</span> (isActiveCircuit()) {
<span class="line-number"> 724</span>                 currentPoint = Grid.snapToGrid(e.getPoint());
<span class="line-number"> 725</span> 
<span class="line-number"> 726</span>                 <span class="comment">// Drop draged components</span>
<span class="line-number"> 727</span>                 <span class="keyword-directive">if</span> (nowDragingComponent) {
<span class="line-number"> 728</span>                     dragActiveSelection(e,<span class="keyword-directive">false</span>,<span class="keyword-directive">true</span>);
<span class="line-number"> 729</span>                 <span class="comment">// Activate all components within the selection box</span>
<span class="line-number"> 730</span>                 } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span> (multipleSelection) {
<span class="line-number"> 731</span>                     setSelectionBox();
<span class="line-number"> 732</span>                     Rectangle selBox = <span class="keyword-directive">new</span> Rectangle(selX, selY, selWidth, selHeight);
<span class="line-number"> 733</span> 
<span class="line-number"> 734</span>                     <span class="keyword-directive">for</span> (SelectableComponent sc : activeComponents) {
<span class="line-number"> 735</span>                         sc.resetDefaultState();
<span class="line-number"> 736</span>                     }
<span class="line-number"> 737</span>                     activeComponents.clear();
<span class="line-number"> 738</span>                     <span class="keyword-directive">for</span> (SelectableComponent sc : drawnComponents) {
<span class="line-number"> 739</span>                         <span class="keyword-directive">if</span> (sc.isFixed() &amp;&amp; sc.containedIn(selBox)) {
<span class="line-number"> 740</span>                             sc.mouseReleased(e);
<span class="line-number"> 741</span>                             activeComponents.add(sc);
<span class="line-number"> 742</span>                         }
<span class="line-number"> 743</span>                     }
<span class="line-number"> 744</span>                     editor.getClipboard().setHasSelection(!activeComponents.isEmpty());
<span class="line-number"> 745</span>                     
<span class="line-number"> 746</span>                     <span class="comment">// Update the current selection options panel</span>
<span class="line-number"> 747</span>                     <span class="keyword-directive">if</span> (activeComponents.size() == 1) {
<span class="line-number"> 748</span>                         editor.setComponent(activeComponents.get(0));
<span class="line-number"> 749</span>                     }
<span class="line-number"> 750</span>                     multipleSelection = <span class="keyword-directive">false</span>;
<span class="line-number"> 751</span>                     
<span class="line-number"> 752</span>                 } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span> (currentTool.equals(<span class="character">&quot;</span><span class="character">Wire</span><span class="character">&quot;</span>) &amp;&amp; !drawnComponents.isEmpty()) {
<span class="line-number"> 753</span>                     Wire w = (Wire) drawnComponents.peek();
<span class="line-number"> 754</span>                     
<span class="line-number"> 755</span>                     <span class="comment">// Start drawing the new wire</span>
<span class="line-number"> 756</span>                     <span class="keyword-directive">if</span>(w.getOrigin().equals(Wire.DEFAULT_ORIGIN()) &amp;&amp; grid.isConnectionPoint(currentPoint)){
<span class="line-number"> 757</span>                         w.setStartPoint(currentPoint);
<span class="line-number"> 758</span>                     <span class="comment">// We have chosen the start point again, remove the wire</span>
<span class="line-number"> 759</span>                     } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span> (w.getOrigin().equals(currentPoint)) {
<span class="line-number"> 760</span>                         drawnComponents.pop();
<span class="line-number"> 761</span>                         drawnComponents.push(<span class="keyword-directive">new</span> Wire(CircuitPanel.<span class="keyword-directive">this</span>));
<span class="line-number"> 762</span>                     } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span> (!w.getOrigin().equals(Wire.DEFAULT_ORIGIN())) {
<span class="line-number"> 763</span>                         <span class="comment">// Should we continue to draw the wire?</span>
<span class="line-number"> 764</span>                         <span class="comment">//      Only if we have not released on a connection point</span>
<span class="line-number"> 765</span>                         <span class="keyword-directive">if</span> (!grid.isConnectionPoint(currentPoint)  || 
<span class="line-number"> 766</span>                                 (grid.isConnectionPoint(currentPoint) &amp;&amp; grid.getConnectionPoint(currentPoint).hasParent(w))) {
<span class="line-number"> 767</span>                             w.addWaypoint(currentPoint);
<span class="line-number"> 768</span>                         } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span> (grid.getConnectionPoint(currentPoint).canConnect(w.getLogicalWire())) {
<span class="line-number"> 769</span>                             w.setEndPoint(currentPoint);
<span class="line-number"> 770</span>                             <span class="comment">// Remove zero-length wires created by wire optimisation</span>
<span class="line-number"> 771</span>                             <span class="keyword-directive">if</span>(w.getOrigin().equals(w.getEndPoint())){
<span class="line-number"> 772</span>                                 drawnComponents.pop();
<span class="line-number"> 773</span>                             } <span class="keyword-directive">else</span> {
<span class="line-number"> 774</span>                                 w.translate(0, 0, <span class="keyword-directive">true</span>);
<span class="line-number"> 775</span>                             }
<span class="line-number"> 776</span>                             drawnComponents.push(<span class="keyword-directive">new</span> Wire(CircuitPanel.<span class="keyword-directive">this</span>));
<span class="line-number"> 777</span>                         }
<span class="line-number"> 778</span>                     }
<span class="line-number"> 779</span>                     <span class="comment">// Highlight connection point?</span>
<span class="line-number"> 780</span>                     <span class="keyword-directive">if</span> (grid.isConnectionPoint(currentPoint) 
<span class="line-number"> 781</span>                             &amp;&amp; grid.getConnectionPoint(currentPoint).noOfConnections() &gt; 1) {
<span class="line-number"> 782</span>                         grid.setActivePoint(currentPoint, <span class="keyword-directive">true</span>);
<span class="line-number"> 783</span>                     }
<span class="line-number"> 784</span>                 } 
<span class="line-number"> 785</span>                 repaint();
<span class="line-number"> 786</span>             }
<span class="line-number"> 787</span>         }
<span class="line-number"> 788</span>     }
<span class="line-number"> 789</span>     
<span class="line-number"> 790</span>     <span class="comment">/**</span> <span class="comment">This</span> <span class="comment">inner</span> <span class="comment">class</span> <span class="comment">handles</span> <span class="comment">all</span> <span class="comment">Mouse</span> <span class="comment">Motion</span> <span class="comment">events</span> <span class="comment">*/</span>
<span class="line-number"> 791</span>     <span class="keyword-directive">public</span> <span class="keyword-directive">class</span> CircuitPanelMouseMotionAdapter <span class="keyword-directive">extends</span> MouseMotionAdapter {
<span class="line-number"> 792</span>         @Override
<span class="line-number"> 793</span>         <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> mouseMoved(MouseEvent e) {
<span class="line-number"> 794</span>             <span class="keyword-directive">if</span>(isActiveCircuit()){
<span class="line-number"> 795</span>                 currentPoint = Grid.snapToGrid(e.getPoint());
<span class="line-number"> 796</span>                 
<span class="line-number"> 797</span>                 <span class="keyword-directive">if</span>(!nowDragingComponent &amp;&amp; !currentTool.equals(<span class="character">&quot;</span><span class="character">Wire</span><span class="character">&quot;</span>)){                
<span class="line-number"> 798</span> 
<span class="line-number"> 799</span>                     <span class="comment">// Moving a new non-fixed component around</span>
<span class="line-number"> 800</span>                     <span class="keyword-directive">if</span>(!drawnComponents.isEmpty() 
<span class="line-number"> 801</span>                             &amp;&amp; !drawnComponents.peek().isFixed() 
<span class="line-number"> 802</span>                             &amp;&amp; !nowDragingComponent){
<span class="line-number"> 803</span>                         SelectableComponent sc = drawnComponents.peek();
<span class="line-number"> 804</span>                         <span class="keyword-directive">int</span> dx = currentPoint.x-sc.getOrigin().x-sc.getCentre().x;
<span class="line-number"> 805</span>                         <span class="keyword-directive">int</span> dy = currentPoint.y-sc.getOrigin().y-sc.getCentre().y;
<span class="line-number"> 806</span>                         <span class="keyword-directive">boolean</span> canMove = grid.canTranslateComponent(sc,dx,dy);
<span class="line-number"> 807</span>                         
<span class="line-number"> 808</span>                         <span class="comment">// Move the component</span>
<span class="line-number"> 809</span>                         <span class="keyword-directive">if</span>(canMove){                     
<span class="line-number"> 810</span>                             sc.translate(dx, dy, <span class="keyword-directive">false</span>);
<span class="line-number"> 811</span>                             sc.mouseMoved(e);                            
<span class="line-number"> 812</span>                         }
<span class="line-number"> 813</span> 
<span class="line-number"> 814</span>                         <span class="comment">// Activate any connection points that overlap pins on the current non-fixed component</span>
<span class="line-number"> 815</span>                         <span class="keyword-directive">for</span>(Pin local: sc.getPins()){
<span class="line-number"> 816</span>                             Point p = local.getGlobalLocation();
<span class="line-number"> 817</span>                             <span class="keyword-directive">if</span>(grid.isConnectionPoint(p)){
<span class="line-number"> 818</span>                                 grid.setActivePoint(p,<span class="keyword-directive">true</span>);
<span class="line-number"> 819</span>                             }
<span class="line-number"> 820</span>                         }                        
<span class="line-number"> 821</span>                         repaintDirtyAreas();
<span class="line-number"> 822</span>                         <span class="keyword-directive">if</span>(canMove){ previousCurrentPoint = currentPoint;}
<span class="line-number"> 823</span> 
<span class="line-number"> 824</span>                     <span class="comment">// Hover highlights    </span>
<span class="line-number"> 825</span>                     } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span> (currentTool.equals(<span class="character">&quot;</span><span class="character">Select</span><span class="character">&quot;</span>)){       
<span class="line-number"> 826</span>                         <span class="comment">// Reset the last component to be hovered over</span>
<span class="line-number"> 827</span>                         temporaryComponent = <span class="keyword-directive">null</span>;
<span class="line-number"> 828</span>                         <span class="keyword-directive">if</span>(getHighlightedComponent()!=<span class="keyword-directive">null</span>){
<span class="line-number"> 829</span>                             getHighlightedComponent().revertHoverState();
<span class="line-number"> 830</span>                             repaint(getHighlightedComponent().getBoundingBox());
<span class="line-number"> 831</span>                         }
<span class="line-number"> 832</span>                         <span class="comment">// Determine which component the mouse lies in</span>
<span class="line-number"> 833</span>                         <span class="keyword-directive">for</span>(SelectableComponent sc: drawnComponents){
<span class="line-number"> 834</span>                             <span class="keyword-directive">if</span>(sc.containsPoint(currentPoint)){
<span class="line-number"> 835</span>                                 temporaryComponent = sc;
<span class="line-number"> 836</span>                                 <span class="keyword-directive">break</span>;
<span class="line-number"> 837</span>                             }                            
<span class="line-number"> 838</span>                         }  
<span class="line-number"> 839</span>                         <span class="comment">// Pass the &quot;Highlight Token&quot;</span>
<span class="line-number"> 840</span>                         <span class="keyword-directive">if</span>(temporaryComponent!=<span class="keyword-directive">null</span>){
<span class="line-number"> 841</span>                             setHighlightedComponent(temporaryComponent);
<span class="line-number"> 842</span>                             temporaryComponent.mouseMoved(e);
<span class="line-number"> 843</span>                             repaint(temporaryComponent.getBoundingBox());
<span class="line-number"> 844</span>                         }                        
<span class="line-number"> 845</span>                     }                                 
<span class="line-number"> 846</span>                 } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span>(currentTool.equals(<span class="character">&quot;</span><span class="character">Wire</span><span class="character">&quot;</span>) 
<span class="line-number"> 847</span>                         &amp;&amp; !drawnComponents.isEmpty()
<span class="line-number"> 848</span>                         &amp;&amp; drawnComponents.peek() <span class="keyword-directive">instanceof</span> Wire){
<span class="line-number"> 849</span> 
<span class="line-number"> 850</span>                     Wire w = (Wire) drawnComponents.peek();
<span class="line-number"> 851</span>                     Rectangle dirtyArea = w.getBoundingBox();
<span class="line-number"> 852</span>                     w.setEndPoint(currentPoint);
<span class="line-number"> 853</span>                     <span class="keyword-directive">if</span>(grid.isConnectionPoint(currentPoint)){
<span class="line-number"> 854</span>                         grid.setActivePoint(currentPoint, <span class="keyword-directive">true</span>);
<span class="line-number"> 855</span>                     }
<span class="line-number"> 856</span> 
<span class="line-number"> 857</span>                     dirtyArea.add(w.getBoundingBox());
<span class="line-number"> 858</span>                     dirtyArea.add(currentPoint);
<span class="line-number"> 859</span>                     repaint(dirtyArea);
<span class="line-number"> 860</span>                 }
<span class="line-number"> 861</span>             } 
<span class="line-number"> 862</span>         }                   
<span class="line-number"> 863</span> 
<span class="line-number"> 864</span>         @Override
<span class="line-number"> 865</span>         <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> mouseDragged(MouseEvent e) {
<span class="line-number"> 866</span>             <span class="keyword-directive">if</span>(isActiveCircuit()){
<span class="line-number"> 867</span>                 currentPoint = Grid.snapToGrid(e.getPoint());
<span class="line-number"> 868</span>                 <span class="keyword-directive">if</span>(currentTool.equals(<span class="character">&quot;</span><span class="character">Select</span><span class="character">&quot;</span>)){
<span class="line-number"> 869</span>                     <span class="comment">// Continue Drag</span>
<span class="line-number"> 870</span>                     <span class="keyword-directive">if</span>(nowDragingComponent){
<span class="line-number"> 871</span>                         dragActiveSelection(e,<span class="keyword-directive">false</span>,<span class="keyword-directive">false</span>);
<span class="line-number"> 872</span>                     }  <span class="keyword-directive">else</span> {
<span class="line-number"> 873</span>                         <span class="comment">// Area we dragging from a fixed component?</span>
<span class="line-number"> 874</span>                         <span class="keyword-directive">boolean</span> clickingEmptySpace = <span class="keyword-directive">true</span>;
<span class="line-number"> 875</span>                         temporaryComponent = <span class="keyword-directive">null</span>;
<span class="line-number"> 876</span>                         <span class="keyword-directive">if</span>(!multipleSelection){
<span class="line-number"> 877</span>                             <span class="keyword-directive">for</span>(SelectableComponent sc: drawnComponents){
<span class="line-number"> 878</span>                                 <span class="keyword-directive">if</span>(sc.isFixed() &amp;&amp; sc.containsPoint(dragStartPoint)){
<span class="line-number"> 879</span>                                     clickingEmptySpace = <span class="keyword-directive">false</span>;
<span class="line-number"> 880</span>                                     temporaryComponent = sc;
<span class="line-number"> 881</span>                                     <span class="keyword-directive">break</span>;
<span class="line-number"> 882</span>                                 } 
<span class="line-number"> 883</span>                             }  
<span class="line-number"> 884</span>                         }                            
<span class="line-number"> 885</span>                         <span class="comment">// Create a selection box or Start dragging selection</span>
<span class="line-number"> 886</span>                         <span class="keyword-directive">if</span>(clickingEmptySpace){
<span class="line-number"> 887</span>                             multipleSelection = <span class="keyword-directive">true</span>;                            
<span class="line-number"> 888</span>                             setSelectionBox();
<span class="line-number"> 889</span>                             repaint();
<span class="line-number"> 890</span>                         } <span class="keyword-directive">else</span> {
<span class="line-number"> 891</span>                             nowDragingComponent = <span class="keyword-directive">true</span>;
<span class="line-number"> 892</span>                             <span class="keyword-directive">if</span>(!activeComponents.contains(temporaryComponent)){ 
<span class="line-number"> 893</span>                                 resetActiveComponents();
<span class="line-number"> 894</span>                                 activeComponents.add(temporaryComponent); 
<span class="line-number"> 895</span>                                 editor.getClipboard().setHasSelection(<span class="keyword-directive">true</span>);
<span class="line-number"> 896</span>                             } <span class="keyword-directive">else</span> {
<span class="line-number"> 897</span>                                 <span class="comment">// Move active dragged components to the top of the stack</span>
<span class="line-number"> 898</span>                                 drawnComponents.removeAll(activeComponents);
<span class="line-number"> 899</span>                                 drawnComponents.addAll(activeComponents);
<span class="line-number"> 900</span>                             }                            
<span class="line-number"> 901</span>                             <span class="comment">// Start drag</span>
<span class="line-number"> 902</span>                             dragActiveSelection(e, <span class="keyword-directive">true</span>, <span class="keyword-directive">false</span>);
<span class="line-number"> 903</span>                        }
<span class="line-number"> 904</span>                     }                    
<span class="line-number"> 905</span>                 } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span> (currentTool.equals(<span class="character">&quot;</span><span class="character">Wire</span><span class="character">&quot;</span>) &amp;&amp; !drawnComponents.isEmpty()){
<span class="line-number"> 906</span>                     Wire w = (Wire) drawnComponents.peek();
<span class="line-number"> 907</span>                     Rectangle dirtyArea = w.getBoundingBox();
<span class="line-number"> 908</span>                     
<span class="line-number"> 909</span>                     <span class="comment">// Start drawing the new wire</span>
<span class="line-number"> 910</span>                     <span class="keyword-directive">if</span>(w.getOrigin().equals(SelectableComponent.DEFAULT_ORIGIN())
<span class="line-number"> 911</span>                             &amp;&amp; grid.isConnectionPoint(dragStartPoint)){
<span class="line-number"> 912</span>                         w.setStartPoint(dragStartPoint);                           
<span class="line-number"> 913</span>                     }
<span class="line-number"> 914</span>                     w.setEndPoint(currentPoint); 
<span class="line-number"> 915</span>                     
<span class="line-number"> 916</span>                     <span class="comment">// Highlight connection point?</span>
<span class="line-number"> 917</span>                     <span class="keyword-directive">if</span>(grid.isConnectionPoint(currentPoint)
<span class="line-number"> 918</span>                             &amp;&amp; grid.getConnectionPoint(currentPoint).noOfConnections() &gt; 1){
<span class="line-number"> 919</span>                         grid.setActivePoint(currentPoint, <span class="keyword-directive">true</span>);
<span class="line-number"> 920</span>                     }                    
<span class="line-number"> 921</span>                     
<span class="line-number"> 922</span>                     dirtyArea.add(w.getBoundingBox());
<span class="line-number"> 923</span>                     dirtyArea.add(currentPoint);
<span class="line-number"> 924</span>                     repaint(dirtyArea);
<span class="line-number"> 925</span>                     lastDragPoint = currentPoint;
<span class="line-number"> 926</span>                 }
<span class="line-number"> 927</span>             }       
<span class="line-number"> 928</span>         }
<span class="line-number"> 929</span>     }    
<span class="line-number"> 930</span>     
<span class="line-number"> 931</span>     <span class="comment">/**</span>
<span class="line-number"> 932</span> <span class="comment">     * </span><span class="comment">Translate</span> <span class="comment">the</span> <span class="comment">active</span> <span class="comment">selection</span> <span class="comment">according</span> <span class="comment">to</span> <span class="comment">start</span> <span class="comment">and</span> <span class="comment">end</span> <span class="comment">points</span> <span class="comment">of</span> <span class="comment">a</span> 
<span class="line-number"> 933</span> <span class="comment">     * </span><span class="comment">drag</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">mouse</span><span class="comment">.</span>
<span class="line-number"> 934</span> <span class="comment">     * </span>
<span class="line-number"> 935</span> <span class="comment">     * </span><span class="ST0">@param</span> <span class="comment">e</span> <span class="comment">The</span> <span class="comment">event</span> <span class="comment">associated</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">drag</span>
<span class="line-number"> 936</span> <span class="comment">     * </span><span class="ST0">@param</span> <span class="comment">start</span> <span class="comment">Are</span> <span class="comment">we</span> <span class="comment">just</span> <span class="comment">starting</span> <span class="comment">the</span> <span class="comment">drag</span><span class="comment">?</span>
<span class="line-number"> 937</span> <span class="comment">     * </span><span class="ST0">@param</span> <span class="comment">finish</span> <span class="comment">Should</span> <span class="comment">we</span> <span class="comment">finish</span> <span class="comment">the</span> <span class="comment">drag</span>
<span class="line-number"> 938</span>      <span class="comment">*/</span>
<span class="line-number"> 939</span>     <span class="keyword-directive">private</span> <span class="keyword-directive">void</span> dragActiveSelection(MouseEvent e, <span class="keyword-directive">boolean</span> start, <span class="keyword-directive">boolean</span> finish) {
<span class="line-number"> 940</span>         <span class="comment">// We don&#39;t want to translate a wire unless we are moving it with some other pieces.</span>
<span class="line-number"> 941</span>         <span class="keyword-directive">if</span>(activeComponents.size() == 1 &amp;&amp; activeComponents.get(0) <span class="keyword-directive">instanceof</span> Wire &amp;&amp; e != <span class="keyword-directive">null</span>){            
<span class="line-number"> 942</span>             <span class="keyword-directive">if</span>(finish){
<span class="line-number"> 943</span>                 activeComponents.get(0).mouseDraggedDropped(e);
<span class="line-number"> 944</span>                 nowDragingComponent = <span class="keyword-directive">false</span>;
<span class="line-number"> 945</span>             } <span class="keyword-directive">else</span> {
<span class="line-number"> 946</span>                 activeComponents.get(0).mouseDragged(e);     
<span class="line-number"> 947</span>             }
<span class="line-number"> 948</span>             repaintDirtyAreas();
<span class="line-number"> 949</span>             lastDragPoint = currentPoint;
<span class="line-number"> 950</span>             previousCurrentPoint = lastDragPoint;
<span class="line-number"> 951</span>         <span class="comment">// Translate selection</span>
<span class="line-number"> 952</span>         } <span class="keyword-directive">else</span> {
<span class="line-number"> 953</span>             Point anchor = <span class="keyword-directive">null</span>;
<span class="line-number"> 954</span>             <span class="keyword-directive">if</span>(temporaryComponent!=<span class="keyword-directive">null</span>){
<span class="line-number"> 955</span>                 anchor = temporaryComponent.getOrigin().getLocation();
<span class="line-number"> 956</span>                 anchor.translate(temporaryComponent.getCentre().x, temporaryComponent.getCentre().y);
<span class="line-number"> 957</span>             }
<span class="line-number"> 958</span>             <span class="comment">// Check whether all the moves can be made</span>
<span class="line-number"> 959</span>             <span class="keyword-directive">boolean</span> canMoveAll = <span class="keyword-directive">true</span>;
<span class="line-number"> 960</span>             <span class="keyword-directive">for</span> (SelectableComponent sc : activeComponents) {
<span class="line-number"> 961</span>                 <span class="keyword-directive">if</span>(anchor==<span class="keyword-directive">null</span>){ 
<span class="line-number"> 962</span>                     anchor = sc.getOrigin().getLocation(); 
<span class="line-number"> 963</span>                     anchor.translate(sc.getCentre().x, sc.getCentre().y);
<span class="line-number"> 964</span>                 }
<span class="line-number"> 965</span>                 <span class="keyword-directive">int</span> dx = currentPoint.x - anchor.x;
<span class="line-number"> 966</span>                 <span class="keyword-directive">int</span> dy = currentPoint.y - anchor.y;
<span class="line-number"> 967</span>                 canMoveAll &amp;= grid.canTranslateComponent(sc, dx, dy);
<span class="line-number"> 968</span>             }
<span class="line-number"> 969</span>             <span class="keyword-directive">if</span> (canMoveAll) {
<span class="line-number"> 970</span>                 SelectionTranslateCommand stc = <span class="keyword-directive">null</span>;
<span class="line-number"> 971</span>                 <span class="keyword-directive">if</span>(finish &amp;&amp; !start){ stc = <span class="keyword-directive">new</span> SelectionTranslateCommand(); }
<span class="line-number"> 972</span>                 <span class="comment">// Perform appropriate actions to the start, middle of end of a translation</span>
<span class="line-number"> 973</span>                 <span class="keyword-directive">for</span> (SelectableComponent sc : activeComponents) {
<span class="line-number"> 974</span>                     <span class="keyword-directive">int</span> dx = currentPoint.x - anchor.x;
<span class="line-number"> 975</span>                     <span class="keyword-directive">int</span> dy = currentPoint.y - anchor.y;
<span class="line-number"> 976</span>                     <span class="comment">// Start</span>
<span class="line-number"> 977</span>                     <span class="keyword-directive">if</span>(start &amp;&amp; !finish){
<span class="line-number"> 978</span>                         sc.translate(currentPoint.x - lastDragPoint.x, currentPoint.y - lastDragPoint.y, <span class="keyword-directive">false</span>);
<span class="line-number"> 979</span>                         sc.mouseDragged(e);
<span class="line-number"> 980</span>                         nowDragingComponent = <span class="keyword-directive">true</span>;
<span class="line-number"> 981</span>                     <span class="comment">// End    </span>
<span class="line-number"> 982</span>                     } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span>(finish &amp;&amp; !start){
<span class="line-number"> 983</span>                         sc.translate(0, 0, <span class="keyword-directive">true</span>);
<span class="line-number"> 984</span>                         stc.translate(sc, dx, dy);
<span class="line-number"> 985</span>                         nowDragingComponent = <span class="keyword-directive">false</span>;
<span class="line-number"> 986</span>                     <span class="comment">// Invalid</span>
<span class="line-number"> 987</span>                     } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span>(start &amp;&amp; finish){
<span class="line-number"> 988</span>                         ErrorHandler.newError(<span class="character">&quot;</span><span class="character">Drag Error</span><span class="character">&quot;</span>, 
<span class="line-number"> 989</span>                                 <span class="character">&quot;</span><span class="character">You cannot start and finish a drag at the same time</span><span class="character">&quot;</span>);
<span class="line-number"> 990</span>                     <span class="comment">// Middle</span>
<span class="line-number"> 991</span>                     } <span class="keyword-directive">else</span> {
<span class="line-number"> 992</span>                         sc.translate(dx, dy, <span class="keyword-directive">false</span>);
<span class="line-number"> 993</span>                         sc.mouseDragged(e);
<span class="line-number"> 994</span>                         nowDragingComponent = <span class="keyword-directive">true</span>;
<span class="line-number"> 995</span>                     }
<span class="line-number"> 996</span>                 }
<span class="line-number"> 997</span>                 <span class="comment">// Save the translation to the command history</span>
<span class="line-number"> 998</span>                 <span class="keyword-directive">if</span>(finish &amp;&amp; !start){
<span class="line-number"> 999</span>                     cmdHist.doCommand(stc);
<span class="line-number">1000</span>                     resetActiveComponents();
<span class="line-number">1001</span>                 }
<span class="line-number">1002</span>                 repaintDirtyAreas();
<span class="line-number">1003</span>                 lastDragPoint = currentPoint;
<span class="line-number">1004</span>                 previousCurrentPoint = currentPoint;
<span class="line-number">1005</span>             <span class="comment">// Catch finish translations if the move was invalid</span>
<span class="line-number">1006</span>             } <span class="keyword-directive">else</span> <span class="keyword-directive">if</span>(finish &amp;&amp; !start) {
<span class="line-number">1007</span>                 SelectionTranslateCommand stc = <span class="keyword-directive">new</span> SelectionTranslateCommand();
<span class="line-number">1008</span>                 <span class="keyword-directive">for</span> (SelectableComponent sc : activeComponents) {
<span class="line-number">1009</span>                     stc.translate(sc, 0, 0);
<span class="line-number">1010</span>                     nowDragingComponent = <span class="keyword-directive">false</span>;
<span class="line-number">1011</span>                 }
<span class="line-number">1012</span>                 cmdHist.doCommand(stc);
<span class="line-number">1013</span>                 resetActiveComponents();
<span class="line-number">1014</span>             }
<span class="line-number">1015</span>         }
<span class="line-number">1016</span>     }
<span class="line-number">1017</span> }
<span class="line-number">1018</span> 
</pre></body>
</html>
